Ask Your Question
1

Iteration inside a LatexExpr

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

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
1

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

slelievre gravatar image

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

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 :}}"),
         x[i])

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

Comments

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

CyrilleP gravatar imageCyrilleP ( 2021-11-20 19:37:41 +0200 )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 +0200 )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

Stats

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

Seen: 139 times

Last updated: Nov 21 '21