[Tex/LaTex] Create list of abbreviations

table of contents

I want to create something similar to \listoffigures, \listoftables etc. What I've done so far:

\def\startloa#1
{
    \begingroup
        \begin{listofabbrv}{SPMD}
        \@input{\jobname.#1}
        \end{listofabbrv}
        \global\@nobreakfalse
    \endgroup
    \newpage
}

\newcommand{\listofabbreviations}
{
    \@startloa{loa}
}

% abvr{abvr}{text}
\def\abvr#1#2
{
    \makeatletter
    \immediate\write\tempfile{\backslash item[#1] #2}
    \makeatother
}

\newwrite\tempfile

\begin{document}

    \immediate\openout\tempfile=\jobname.loa

    \listofabbreviations

    \abvr{NULL}{/dev/null}

    \immediate\closeout\tempfile

\end{document}

The \begin/\end{listofabbvr} is predefined in the style I'm using, so I can't change it. Is it done this way:

\begin{listofabbrv}{SPMD}
    \item[NULL] /dev/null
\end{listofabbrv}

The main problems:

  • The \abvr command does not work. The file output is:

    \delimiter "026E30F item[NULL] /dev/null
    

    instead of

    \item[NULL] /dev/null
    
  • The command \listofabbreviations does not work. Is there something wrong with \def\startloa? A little snippet of the output log:

    ERROR:
    74  ! You can't use `\spacefactor' in vertical mode.
    75  \@->\spacefactor 
    76                   \@m 
    77  l.133   \listofabbreviations
    78                             
    79  ! Missing $ inserted.
    --- x --- x ---
    ERROR:
    79  ! Missing $ inserted.
    80  <inserted text> 
    81                  $
    82  l.133   \listofabbreviations
    83                             
    84  ! Missing $ inserted.
    --- x --- x ---
    ERROR:
    84  ! Missing $ inserted.
    85  <inserted text> 
    86                  $
    87  l.134 
    88        
    89  [3]
    --- x --- x ---
    

Best Answer

There are many problems in your code. TeX is not C, and

\def\x#1
{
  ...
}

is very different from

\def\x#1{...}

Here is a way to produce your list

\documentclass[a4paper]{article}
\makeatletter
\newcommand\@startloa{%
  \begin{listofabbrv}{SPMD}
  \InputIfFileExists{\jobname.loa}{}{\item[\null]}
  \end{listofabbrv}
  \newwrite\@loa
  \immediate\openout\@loa=\jobname.loa
}
\newcommand{\listofabbreviations}{\@startloa}

% abvr{abvr}{text}
\def\abvr#1#2{\immediate\write\@loa{\unexpanded{\item[#1]#2}}}
\makeatother

\newenvironment{listofabbrv}[1]{\begin{itemize}}{\end{itemize}}

\begin{document}
\listofabbreviations

\abvr{NULL}{/dev/null}

\end{document}

The \newenvironment line is just to have a listofabbrv environment.