# How to understand and write LaTeX code?

LaTeX is like a language. In order to write LaTeX code I have to understand the "word" and the "sentences structure". I have searched on wiki, some LaTeX books, but they just give the LaTeX command, not how to write a LaTeX code for some specific purpose or to understand the LaTeX code.

Here is one example:

\renewenvironment{svgraybox}%
{\fboxsep=12pt\relax
\list{}{\leftmargin=12pt\rightmargin=2\leftmargin\leftmargin=\z@\topsep=\z@\relax}%
\expandafter\item\parindent=\svparindent
\hskip-\listparindent}%
%
\renewenvironment{svtintedbox}%
{\fboxsep=12pt\relax
\begin{tinted}%
\list{}{\leftmargin=12pt\rightmargin=2\leftmargin\leftmargin=\z@\topsep=\z@\relax}%
\expandafter\item\parindent=\svparindent
\relax}%
{\endlist\end{tinted}}%
%
}}
%
\let\SVMonoOpt\@empty


I do not know what is \z@, what is \relax, what is \fboxsep ... and if I want to design my LaTeX file in my own form, I know that I have to understand many other things.

Note that I can write simple LaTeX enviroment in mathematics (like the matrix, align, enviroment) but it does not help me to understand the above code.

So, what could I do? What book should I read? Or what programming language should I learn?

• A tip: If you indent lines by 4 spaces, then they are marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it). – Mico Nov 12 '13 at 8:17
• I would suggest you take a look at the posting Where do I start LaTeX programming? and the associated answers. – Mico Nov 12 '13 at 8:22
• I think you should start with the TeXbook by Donald Knuth. It explains things like relax and the @ symbol. TeX is a programming language by itself. – Benjamin McKay Nov 12 '13 at 8:23
• @BenjaminMcKay Thanks. I have looked at Knuth's book. But there are a lot of informations that lie out side that book. If I remember correctly, that book does not say much about parameter :) – Knumber10 Nov 12 '13 at 8:34
• Run texdoc source2e from the command prompt (or use the gui alternative texdoctk). That has all the LaTeX command definitions. – Nicola Talbot Nov 12 '13 at 9:14

The TeXbook is the best place to learn about the underlying TeX language, although TeX-by-Topic (texdoc texbytopic) in most distributions is a good free alternative. But much of what you show is not directly using TeX primitives but using constructs such as \list defined in the latex format. The LaTeX Companion has detail for that or the free documented sources of LaTeX texdoc source2e has lots of information.

This site of course can also be used. For example

• What I want for Christmas this year is ‘Tex and LaTex Programming for Dummies’ by David Carlisle. =8-) – Sveinung Nov 12 '13 at 10:35
• @DavidCarlisle You should write it. I (and many others) would buy it. – Nicholas Hamilton Nov 12 '13 at 13:06

You can find out what any command does by asking LaTeX itself. Just open up a console window and run latex (or whatever variant you prefer), and then you can issue commands line by line.

For example, to find out what \z@ means, you can use

\$ latex
**\makeatletter
*\show\z@


and LaTeX will give you

> \z@=\dimen12.


(The \makeatletter command allows you to use the @ sign in commands.)

If you want to find out what length is stored in \dimen12, you can then use

*\showthe\dimen12


which will give you

> 0.0pt


So the \z@ command is a short hand for a zero length.

The \show command will tell you the definition of any command except a primitive; you can look those up online with a Google search.

• Thank ChrisS for your answer. I need to understand the LaTex essentially, systematically, but I still do not know what is \show, \makeatletter,... and there will be many other things. So thank for your answer, but I do not think that is a good idea for my case :) – Knumber10 Nov 12 '13 at 8:47
• This is a valid solution when you know a command and you wanna check what it does. But what about the contrary? What if you want to do something and I need to know the command? Please note that this is no attack, I really like your answer (Infact I voted it up). But as a beginner latex enthusiast I exactly understand what the questioner is talking about! I think the question is not about a specific command but a general approach. – Pouya Nov 12 '13 at 9:07
• @Pouya you really need a book "latex by topic" (by analogy with "tex by topic"). sadly, there is no such thing; and "just now", with the work on a new latex (latex 3) underway, is probably not the time to start. for myself, i learned latex by reading sources (originally latex2.09, back in the 90s) and have learned more as latex has evolved. – wasteofspace Nov 12 '13 at 11:42