Ask Your Question

Generating dynamic latex

asked 2012-01-20 14:58:31 -0500

stef gravatar image

updated 2012-01-20 14:59:04 -0500

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.


edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted

answered 2012-01-22 22:54:07 -0500

pang gravatar image

You can use

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

as explained in this answer to a similar question.

edit flag offensive delete link 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?

stef gravatar imagestef ( 2012-01-23 04:00:51 -0500 )edit

answered 2012-01-21 03:44:54 -0500

Does the \sage command do what you want?

Did you know that $20! = \sage{factorial(20)}$?
edit flag offensive delete link 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!

stef gravatar imagestef ( 2012-01-23 04:01:58 -0500 )edit

answered 2012-01-20 17:54:49 -0500

Shashank gravatar image

updated 2012-01-20 18:11:19 -0500

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.


Also there is a command latex. For eg.


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

edit flag offensive delete link 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}""",{})

stef gravatar imagestef ( 2012-01-20 20:51:10 -0500 )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


Asked: 2012-01-20 14:58:31 -0500

Seen: 541 times

Last updated: Jan 22 '12