# Generating dynamic latex

Is there any way to dynamically generate $\LaTeX$ code in a block preceeded by %latex? This could be useful for taking a data structure and iterating through it to create a data table using the corresponding data.

Thanks!

edit retag close merge delete

Sort by » oldest newest most voted

You can use

Latex().eval(my_string, globals=globals)


as explained in this answer to a similar question.

more

That's it! Thanks! I was able to dynamically generate a table by iterating over data and constructing the corresponding formatting. I was able to use the globals dictionary to pass data in so that it can be rendered inside of \sage{} calls, but what purpose does the locals argument serve?

( 2012-01-23 04:00:51 -0600 )edit

Does the \sage command do what you want?

%latex
Did you know that $20! = \sage{factorial(20)}$?

more

This was close, however you can't generate arbitrary LaTeX code with the \sage command. latex.eval() ended up being what I was looking for. Thanks for your input though!

( 2012-01-23 04:01:58 -0600 )edit

Have you tried sagetex? What you can do is install sagetex and then when you latex your .tex file you can include sage commands inside the tex file. Sagetex will solve the equations and latex them for you.

Edit:

Also there is a command latex. For eg.

alpha=var('alpha')
latex(sin(alpha))


gives you the latex output for the expression which you can copy paste in to you latex document.

more

Oh - I'd like to try and do it within the context of notebook. I'm just trying to demo Sage for use in connecting to Apache Hive to generate summary reports on data. I've been able to generate some limited success by calling latex.eval("...") where the string can be dynamically constructed with data. It also helps to pass in data via the locals construct, allowing for that data to be referenced inside \sage{} blocks. However, the rendering of latex.eval() has been somewhat subpar. For example: latex.eval(r""" \documentclass[12pt]{article} \usepackage[margin=0.20in,landscape]{geometry} \usepackage{colortbl} \usepackage[table]{xcolor} \begin{document} \begin{tabular}{ cccccccccc } \hline \rowcolor[rgb]{0.21,0.37,0.58}... \end{tabular} \end{document}""",{})

( 2012-01-20 20:51:10 -0600 )edit