3

How I can put a frame around the grammar syntax in LaTeX like in listings that we use frame = single? For example, for this grammar:

\documentclass{article}
\usepackage{syntax}

\begin{document}
\begin{grammar}

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

<stat-list> ::= <statement> `;' <stat-list> | <statement> 

\end{grammar}
\end{document}

Thank you for any help!

2 Answers 2

3

A way with framed package:

\documentclass{article}
\usepackage{syntax}
\usepackage{framed}
\begin{document}
\begin{framed}
\begin{grammar}

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

<stat-list> ::= <statement> `;' <stat-list> | <statement> 

\end{grammar}
\end{framed}
\end{document}

enter image description here

1
  • Welcome! Happy TeXing
    – koleygr
    Feb 17, 2019 at 13:13
3

The very basic approach without packages: a minipage with a \fbox{}:

\documentclass{article}
\usepackage{syntax}
\begin{document}
\fbox{\begin{minipage}{9cm}
\begin{grammar}
<statement> ::= <ident> `=' <expr> 
\alt `for' <ident> `=' <expr> `to' <expr> `do' <statement> 
\alt `{' <stat-list> `}' 
\alt <empty> 

<stat-list> ::= <statement> `;' <stat-list> | <statement> 
\end{grammar}
\end{minipage}}
\end{document}
2
  • thanks for this additional approach! But, the first solution with framed is keeping the frame within the margins, ie., no need to adjust something. Feb 20, 2019 at 11:58
  • @user4712458 That requirement was not in the question, but this approach also can adjust automatically to the margins. Try with \noindent\fbox{\begin{minipage}{\dimexpr\linewidth-2\fboxsep-2\fboxrule} (your code) \end{minipage}} ;-)
    – Fran
    Feb 20, 2019 at 22:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .