1

I would like to write out some data dictionaries for a program design document in LaTeX, and I am looking for an efficient way to do this.

I want to write snippets that look like:

Name      = FirstName + (Initial) + LastName
FirstName = 1{Character}32
Initial   = Character
LastName  = 1{Character}
Character = [ A-Z | a-z | 0-9 | ' | - ]

It's important that these are lined up at the = signs - they look ugly without it.

I could write this as the MWE:

\documentclass{article}
\usepackage{amsmath}

\begin{document}

\begin{align*}
    \text{Name}      &= \text{FirstName + (Initial) + LastName} \\
    \text{FirstName} &= \text{1\{Character\}32} \\
    \text{Initial}   &= \text{Character} \\
    \text{LastName}  &= \text{1\{Character\}} \\
    \text{Character} &= \text{[ A -- Z $|$ a -- z $|$ 0 -- 9 $|$ ' $|$ -- ]} \\
\end{align*}
\end{document}

but this feels pretty clunky with all of the \text{} commands. Is there a simpler method?

  • 1
    Would a verbatim environment do? Try placing that first snippet you show between \begin{verbatim} and \end{verbatim}. – Torbjørn T. Jun 13 '15 at 18:05
  • @TorbjørnT. Actually, that looks perfect! Why didn't I think of that? (It also solves my un-asked problem of writing out those vertical bars; two birds, one stone.) – Greg d'Eon Jun 13 '15 at 18:09
3

If you want to leave alignment to LaTeX rather than spacing manually in verbatim, you can do something like this with tabular (or longtable if the dictionary is long):

\documentclass{article}
\begin{document}
\begin{tabular}{l@{~=~}l}
Name & FirstName + (Initial) + LastName\\
FirstName & 1\{Character\}32\\
Initial & Character\\
LastName & 1\{Character\}\\
Character & [ A-Z | a-z | 0-9 | ' | - ]\\
\end{tabular}
\end{document}

The point of including non-breaking spaces in the table specification is to keep the equals signs and the text apart. Tables are also easier to customise if you want to change the layout.

If you want to use a typewriter font, simply put a \ttfamily in front of the start of tabular and enclose them in braces.

  • 2
    Add \usepackage[T1]{fontenc} to fix the printing of the pipe character, which display as em dashes with your code. (Cf. How to insert pipe symbol in (La)TeX?) – Torbjørn T. Jun 13 '15 at 19:57
  • @TorbjørnT. That's right. Interestingly, adding \ttfamily fixes this issue as well, but I don't see why. – Attila Szabó Jun 13 '15 at 19:59
  • I like this a lot. I'm now using this as \newenvironment{ddict} {\begin{center}\begin{tabular}{l@{~=~}l}} {\end{tabular}\end{center}} – Greg d'Eon Jun 15 '15 at 0:28
3

When you have the dictionaries already lined up as in your example, an easy way is to use a verbatim environment.

enter image description here

\documentclass{article}
\begin{document}
\begin{verbatim}
Name      = FirstName + (Initial) + LastName
FirstName = 1{Character}32
Initial   = Character
LastName  = 1{Character}
Character = [ A-Z | a-z | 0-9 | ' | - ]
\end{verbatim}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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