Ask Your Question

Generating dynamic latex

asked 2012-01-20 21:58:31 +0200

stef gravatar image

updated 2012-01-20 21:59:04 +0200

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-23 05:54:07 +0200

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 11:00:51 +0200 )edit

answered 2012-01-21 10:44:54 +0200

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 11:01:58 +0200 )edit

answered 2012-01-21 00:54:49 +0200

Shashank gravatar image

updated 2012-01-21 01:11:19 +0200

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-21 03:51:10 +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


Asked: 2012-01-20 21:58:31 +0200

Seen: 1,027 times

Last updated: Jan 23 '12