# lineno and syntax package incompatibilities

I'm using the syntax package from the mdwtools version 1996/05/17 1.07. I successfully set the properties to show a grammar definition properly but then when I try to combine it with the lineno package I got a nasty error showing a double line number for each new rule. Then digging the code of syntax.sty I found I can fix the problem in the definition of 'grammar' commenting this line:

\vskip-\baselineskip%


Now it works better but I have extra new lines between rules. In fact the definitions start at line number 2 and I cannot remove these extra lines! (It seems is related to the lists command) I suppose this is a bug. TeX is far out from my expertize so I report this bug wondering if someone have a workaround or help me to fix this issue.

-
Welcome to TeX.sx, please provide a minimal working example of a document that we could compile that illustrates this error: this helps understand what the problem is, and possibly, how to fix it. – Seamus Dec 31 '10 at 15:29
See here for details of what an MWE is: meta.tex.stackexchange.com/q/228/215 – Seamus Dec 31 '10 at 15:30

Here's a solution:

\documentclass{article}
\usepackage{syntax}
\usepackage{lineno}

\makeatletter
\def\gr@implitem<#1> #2 {%
\sbox\z@{\hskip\labelsep\grammarlabel{#1}{#2}}%
\strut\@@@par% lineno.sty redefines \@@par which was in the original code
\vspace{-\parskip}%
\vspace{-\baselineskip}%
\hrule\@height\z@\@depth\z@\relax%
\item[\unhbox\z@]%
\catcode\<\active%
}
\makeatother
\begin{document}
\begin{linenumbers}
\begin{grammar}

<statement> ::= <ident> ‘=’ <expr>
\alt ‘for’ <ident> ‘=’ <expr> ‘to’ <expr> ‘do’ <statement>
\alt ‘{’ <stat-list> ‘}’
\alt <empty>
<stat-list> ::= <statement> ‘;’ <stat-list> | <statement>

<statement> ::= <ident> ‘=’ <expr>
\alt ‘for’ <ident> ‘=’ <expr> ‘to’ <expr> ‘do’ <statement>
\alt ‘{’ <stat-list> ‘}’
\alt <empty>

\end{grammar}
\end{linenumbers}
\end{document}


Explanation: lineno.sty counts lines based on the \par macro which is internally in LaTeX \@@par. lineno.sty needs to use its own version of that, \@@@par which keeps track of spurious blank lines. Replacing syntax.sty use of \@@par with \@@@par fixes the problem.

-
Great! This solves the problem. Now I just need to change the space between definitions. Thanks. – jrbalderrama Jan 17 '11 at 12:57

Why don't you use one paclage like listings for example ?

\documentclass{article}
\usepackage{listings}

\begin{document}

\begin{lstlisting}

<statement> ::= <ident> = <expr>
|  for <ident> = <expr> to <expr> do <statement>
|    <stat-list>
|  <empty>
<stat-list> ::= <statement> ; <stat-list> | <statement>

<statement> ::= <ident> = <expr>
|  for <ident> = <expr> to <expr> do <statement>
|    <stat-list>
|  <empty>
\end{lstlisting}

\end{document}

-
Although listings could be made to emulate the syntax package fairly easily, out of the box your example doesn't format the grammar very nicely compared to the syntax package. You also forgot to number the lines in your example, which was the point of the question. Can listings` ignore blank lines in its numbering system? – Alan Munn Nov 26 '11 at 16:09