tags:

views:

136

answers:

2

I'm currently writing an API document in LaTeX. I have a tabular environment with a list of error codes and descriptions, like so:

\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & (description of error 1) \\ \hline
2 & (description of error 2) \\ \hline

\end{tabular}

At various places later in the document, I reference an error's code and its description together, like so:

Possible error conditions:
\begin{itemize}
\item 1---(description of error 1)

\end{itemize}

I want to automate this process so I don't have to retype the descriptions every time. I have tried using a counter, labels, and the \savebox command, but it's pretty cumbersome:

\newcounter{error}

% Inside the tabular environment:

\newsavebox{\ErrorOne}
\savebox{\ErrorOne}{(description of error 1)}
\refstepcounter{error} \label{ErrorOne} \arabic{error} & \usebox{\ErrorOne} \\ \hline

and later, to reference the error,

\ref{ErrorOne}---\usebox{\ErrorOne}

I particularly object to having to use ErrorOne for the labels but \ErrorOne (with the leading backslash) for the saveboxes. I also don't really want names like ErrorOne, since I might need to change the order at some point. What I want is to be able to define a few commands:

\newerror{errorlabel}{Description}  % defines the error (doesn't output anything)
\errorcode{errorlabel}              % outputs the error code
\errordesc{errorlabel}              % outputs the error description

and then be able to say something like

\newerror{ArgumentError}{Too many arguments}
\newerror{DatabaseError}{Could not connect to database}

% Inside the tabular environment:

\errorcode{ArgumentError} & \errordesc{ArgumentError} \\ \hline
\errorcode{DatabaseError} & \errordesc{DatabaseError} \\ \hline

% Later on:

\errorcode{DatabaseError}---\errordesc{DatabaseError}

with the error codes (1, 2, 3, ...) being automatically generated like labels would be.

Any ideas?

A: 

In your preamble, create new commands for each error, then just call the command:

\newcommand{\errorone}{this is the error and its description}

then in the body, just call the new command:

\begin{tabular}{|c|l|}
\hline
\textbf{Code} & \textbf{Description} \\ \hline
1 & \errorone \\ \hline
Mica
Thanks... that hard-coded 1 is bothering me, though. I guess I didn't make it clear in the question that I would also like to be able to change the order easily. I'll edit that. Still, the different-command-for-each-error idea does look simpler than saveboxes. Maybe this is a partial solution.
DLosc
Mica
+2  A: 

The following works for me

\catcode`\@=11

\newcounter{error}
\def\newerror#1#2{\refstepcounter{error}%
   \expandafter\xdef\csname errno@#1\endcsname{\arabic{error}}%
   \expandafter\xdef\csname errds@#1\endcsname{#2}%
}
\def\errorcode#1{\expandafter\printerrinfo \csname errno@#1\endcsname}
\def\errordesc#1{\expandafter\printerrinfo \csname errds@#1\endcsname}
\def\printerrinfo#1{\ifx#1\relax\errmessage{Error code is invalid}%
        \else\expandafter#1\fi}

\catcode`\@=12


\newerror{ArgumentError}{Too many arguments} 
\newerror{DatabaseError}{Could not connect to database} 


\errorcode{DatabaseError}---\errordesc{DatabaseError} 
Alexey Malistov
I have no idea how that works, but it does work. Thanks!
DLosc