ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 29 Jan 2018 11:22:17 -0600Printing latexhttp://ask.sagemath.org/question/40849/printing-latex/I am using sage and would like to implement a function that prints a variable in latex, so I can copy and paste it directly into my latex file. However I am unsure how to do this. This is what I have attempted:
def printlatex(a):
return '$', latex(a), '$'
a = 1*2
printlatex(a)
This returns the tuple `('$', 2, '$')`, which I don't want.
How would I implement the function so it returns `$2$`Mon, 29 Jan 2018 08:58:25 -0600http://ask.sagemath.org/question/40849/printing-latex/Comment by dan_fulea for <p>I am using sage and would like to implement a function that prints a variable in latex, so I can copy and paste it directly into my latex file. However I am unsure how to do this. This is what I have attempted:</p>
<pre><code>def printlatex(a):
return '$', latex(a), '$'
a = 1*2
printlatex(a)
</code></pre>
<p>This returns the tuple <code>('$', 2, '$')</code>, which I don't want.</p>
<p>How would I implement the function so it returns <code>$2$</code></p>
http://ask.sagemath.org/question/40849/printing-latex/?comment=40853#post-id-40853A plus instead of the comma...Mon, 29 Jan 2018 10:51:20 -0600http://ask.sagemath.org/question/40849/printing-latex/?comment=40853#post-id-40853Answer by slelievre for <p>I am using sage and would like to implement a function that prints a variable in latex, so I can copy and paste it directly into my latex file. However I am unsure how to do this. This is what I have attempted:</p>
<pre><code>def printlatex(a):
return '$', latex(a), '$'
a = 1*2
printlatex(a)
</code></pre>
<p>This returns the tuple <code>('$', 2, '$')</code>, which I don't want.</p>
<p>How would I implement the function so it returns <code>$2$</code></p>
http://ask.sagemath.org/question/40849/printing-latex/?answer=40858#post-id-40858Use Python string formatting.
Find more information about Python string formatting at [https://pyformat.info](pyformat.info).
For your example, you could use: `'${a}$'.format(a=2)`.
Mon, 29 Jan 2018 11:22:17 -0600http://ask.sagemath.org/question/40849/printing-latex/?answer=40858#post-id-40858