Ask Your Question

Iteration inside a LatexExpr

asked 2021-11-20 18:51:50 +0100

CyrilleP gravatar image

For presentation reason I have a show like the following one :

show(LatexExpr(r"\text{The variable } x[i] \text{ takes the value :}"), x[i])

How could I have an iteration on on I to display in the text say $x_1$, ...,$x_{10}$ in place of the x[i].

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-11-20 19:12:44 +0100

slelievre gravatar image

updated 2021-11-21 19:18:39 +0100

Something like this:

x = [i^2 for i in range(20)]  # for testing purposes
for i in range(1, 11):
    show(LatexExpr(fr"\text{{The variable }} x_{{{i}}} \text{{ takes the value :}}"),

Python formatted strings or f-strings can include things in {}.

Escaping that inclusion mechanism to get actual braces is done using {{}}.

edit flag offensive delete link more


What means the f in front of the r and why the \text need double {{

CyrilleP gravatar imageCyrilleP ( 2021-11-20 19:37:41 +0100 )edit

The r stands for raw string and f for f-string. See here for the later case.

cav_rt gravatar imagecav_rt ( 2021-11-21 18:09:07 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2021-11-20 18:51:50 +0100

Seen: 38 times

Last updated: Nov 21