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

2 Answers2

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}
Torbjørn T.
  • 206,688
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.