Welcome to Cadabra Q&A, where you can ask questions and receive answers from other members of the community.
+1 vote

Hi everyone! I would like to replace the following list of statements

\a0{#}::LaTeXForm("\alpha_0").
\a1{#}::LaTeXForm("\alpha_1").
\a2{#}::LaTeXForm("\alpha_2").
\a3{#}::LaTeXForm("\alpha_3").
\a4{#}::LaTeXForm("\alpha_4").
\a5{#}::LaTeXForm("\alpha_5").
\a6{#}::LaTeXForm("\alpha_6").
\a7{#}::LaTeXForm("\alpha_7").
\a8{#}::LaTeXForm("\alpha_8").
\a9{#}::LaTeXForm("\alpha_9").
...
\a20{#}::LaTeXForm("\alpha_20").

with a cycle which, given to each step the variable i = 0, ..., 20, iteratively evaluates the declaration.

Initially I tried to write the iterative declaration as a string and then to submit it to an eval(), but it doesn't work. Can anyone help me?

Thanks, Mattia

in General questions by (410 points)

1 Answer

+3 votes

You can't do this with a straightforward eval, because that function does not pre-parse the Cadabra input into Python input. But if you realise that the :: notation gets translated into a simple function call, then it's a relatively simple thing to do yourself:

for i in range(20):
    LaTeXForm(Ex(r'\a'+str(i)+r'{#}'), Ex(r'"\alpha_{'+str(i)+r'}"') )

The only tricky thing here is the quotes in the 2nd Ex: oyu need the single quote to generate a string, and then the double quote inside that so that Cadabra's LaTeXForm property gets fed a string too, not a maths expression.

Granted, this would be nice to have in a cleaner notation, but it gets the job done.

by (83.1k points)

Thank you Kasper!

Excellent! I was trying a similar approach, but never go it right! hahaha

Hi everyone!

how a similar approach can be used to cycle the following statements?

test0:=\a0{\mu}=\b0{\mu}:
test1:=\a1{\mu}=\b1{\mu}:
test2:=\a2{\mu}=\b2{\mu}:
test3:=\a3{\mu}=\b3{\mu}:
...
test20:=\a20{\mu}=\b20{\mu}:

Thanks, Mattia

...