Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I can't manage to add a caption and a label to my grammar environment:

\documentclass{article}

\usepackage{syntax}
\grammarindent 80pt

\begin{document}

\begin{grammar}
<condition> ::= "$" <element> "$ = " <value>

<element> ::= <field> \alt <property>
\end{grammar}

\end{document}
share|improve this question

1 Answer 1

up vote 8 down vote accepted

The {grammar} environment is not a floating environment and thus can't have a caption and cannot be referred to via \label and \ref. From what I can see the syntax package doesn't provide a floating equivalent, either.

What you can do, though, is define your own floating environment. There are a number of packages that can help with this task, one of them being newfloat (which plays nicely together with the caption package - it's from the same author).

\documentclass{article}
\usepackage{newfloat}
\usepackage{syntax}

% declare the floating environment {Grammar}
% this will also define \listofGrammars:
\DeclareFloatingEnvironment[
  % the file extension for the file used to create the list:
  fileext   = logr,% don't use log here!
  % the heading for the list:
  listname  = {List of Grammars},
  % the name used in captions:
  name      = Grammar,
  % the default floating parameters if the environment is used
  % without optional argument:
  placement = htp
]{Grammar}

\grammarindent=80pt

\begin{document}

\begin{Grammar}
 \begin{grammar}
  <condition> ::= "$" <element> "$ = " <value>

  <element> ::= <field> \alt <property>
 \end{grammar}
 \caption{A sample caption}\label{gra:sample}
\end{Grammar}

See grammar~\ref{gra:sample} ...

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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