TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A year ago, I worked for a company doing .NET to create a customer report. They use DevExpress to generate a report based on customers' requirements. However, I realized the way it works is just painful and very time-consuming, because everytime they changed the requirement, we have to change a layout a bit due the the font and text data. At that time, I knew nothing a Latex, but I always think there must be a way to work smarter.
Today, while playing with TikZ package, I realized that Latex is just like another programming language, it's super powerful and flexible in term of drawing figure, positioning text, etc... But I'm not sure if we can use a variable inside Latex or not, for example, if I created a layout, says:

x^2 + 2x = A

Then is it possible for A to be a variable so that every time I need to pull out a new data from Database, I can just fit them in that particular position. I've just learned Latex for couple months, so there are many things that are new to me. In advance, I apologize if my question sounds a little insane or nonsense. Lastly, thank you for your time reading.

share|improve this question
You are looking for the concept of TeX macros. You should look for \newcommand in your favorite LaTeX manual/documentation. – Caramdir Jul 24 '11 at 21:39
@Caramdir: Thanks a lot. – Chan Jul 24 '11 at 21:41
up vote 4 down vote accepted

Best way in my opinion would be to use a template.tex:

$x^2 + 2x = \A$
This is some other data: \B.

And to produce a little document42.tex file for every document you want to generate, like this:


just fill the with appropriate data.

share|improve this answer
Thanks a lot. Your solution looks very promising ;). – Chan Jul 24 '11 at 21:35
As Caramdir pointed out, use \newcommand instead of \def to achieve more than just simple substitutions. – Stéphane Gimenez Jul 24 '11 at 21:48
And use \input instead of \include in this case. – xport Jul 24 '11 at 21:51
Indeed – Stéphane Gimenez Jul 24 '11 at 21:59
And when you feel comfortable with that, proceed in the way of LaTeX;) - learn to write packages (see clsguide.pdf in your TeX distribution, and possibly these questions here: tex.stackexchange.com/questions/2416/… tex.stackexchange.com/questions/528/style-class-tutorials tex.stackexchange.com/questions/8750/make-your-own-sty-files); then you might want to master one (or more) of a few packages supporting the key-value input, etc... – mbork Jul 24 '11 at 23:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.