7

Hi I need to give the syntax of a formula as a figure. How can I format it

\begin{figure}[h]
Formula $\to$ PrimitiveFormula $\vert$ (Formula Connective Formula)
$\vert \neg$ Sentence $\vert$ Quantifier Variable Formula
Primitive Formula $\to$ Predicate(Term,\cdots ,Term)
\end{figure}

Syntax as a figure

  • I figured that I could use a table . It seems much simpler than using spaces and indentation for the formula as a figure. – GermanShepherd Dec 18 '14 at 6:33
  • Did you mean to write table or tabular? Note that in LaTeX parlance, tables and figures are both "floating objects". Either one could be used, in principle, to house the formula. – Mico Dec 18 '14 at 6:40
  • I don't think having the alternative `` directly below the arrows is good formatting; they are "one nesting depth in". I'd indent them a bit more. – Raphael Dec 18 '14 at 17:08
7

This typesetting task seems to be handled most easily with a tabular environment. In the code below, I load the array package to set up separate formatting directives for columns 1 and 3 (automatic italics mode) and for column 2 (automatic math mode). Doing this makes it unnecessary to insert lots of $ symbols and \itshape directives in the body of the tabular environment.

The cfollowing example also uses an optional \fbox directive to visually offset the contents of the tabular environment from its surroundings.

enter image description here

\documentclass{article}
\usepackage{array,caption}
\begin{document}
\begin{figure}
\caption{A formula} \label{fig:formula}
\centering
\fbox{%  % framebox is optional
\begin{tabular}{ >{\itshape}r >{$}c<{$} >{\itshape}l }
Formula & \to & PrimitiveFormula\\
 & \vert & $($Formula Connective Formula$)$ \\
 & \vert & $\lnot$ Sentence \\
 & \vert & Quantifier Variable Formula \\[2ex]
PrimitiveFormula & \to & Predicate$($Term,\dots,Term$)$ \\[2ex]
Term & \to & Function$($Term,\dots,Term$)$ \\
 & \vert & Constant \\
 & \vert & Variable \\
\end{tabular}}
\end{figure}

A cross-reference to figure \ref{fig:formula}.
\end{document} 
7

You can use align* for this.

\documentclass{article}
\usepackage{mathtools,calc}

\newcommand{\ent}[1]{\mathit{#1}} % `entity'
\newcommand{\alt}[1][\to]{\mathrel{\mathmakebox[\widthof{$#1$}]{\vert}}}

\begin{document}

\begin{table}
\caption{Recursive definition of a formula} \label{fig:formula}

\begin{align*}
\ent{Formula} &\to \ent{PrimitiveFormula}\\
 & \alt (\ent{Formula} \mathbin{\ent{Connective}} \ent{Formula}) \\
 & \alt \lnot \ent{Sentence} \\
 & \alt \ent{Quantifier} \, \ent{Variable} \, \ent{Formula} \\[2ex]
\ent{PrimitiveFormula} & \to \ent{Predicate}(\ent{Term},\dots,\ent{Term}) \\[2ex]
\ent{Term} & \to \ent{Function}(\ent{Term},\dots,\ent{Term}) \\
 & \alt \ent{Constant} \\
 & \alt \ent{Variable}
\end{align*}
\end{table}

A cross-reference to table \ref{fig:formula}.
\end{document}

enter image description here

Here's a different implementation, where alignment is not preserved, which in my opinion is better, because it underlines the independence of the various parts. Also the input syntax is easier.

The default value for the separator is the semicolon, but you can use any character that doesn't appear in the second mandatory argument (I used an instance in the first \syntax command just for giving an example, the semicolon would be as good).

\documentclass{article}
\usepackage{xparse,amsmath,array}

\newcommand{\ent}[1]{\mathit{#1}}

\ExplSyntaxOn

\NewDocumentEnvironment{syntax}{ }
 { \use:c {align*} }
 { \use:c {endalign*} }

\NewDocumentCommand{\syntaxitem}{O{;}mm}
 { % #1 is the separator
   % #2 is the defined term
   % #3 is a #1 separated list of alternatives
  \syntax_format_syntax:nnn { #1 } { #2 } { #3 }
 }

\seq_new:N \l__syntax_item_seq

\cs_new_protected:Npn \syntax_format_syntax:nnn #1 #2 #3
 {
  & % for the align*
  \seq_set_split:Nnn \l__syntax_item_seq { #1 } { #3 }
  \begin{array}{@{} r @{} >{{}}c<{{}} @{} l @{} }
  #2 & \to & \seq_use:Nn \l__syntax_item_seq { \\ & \vert & }
  \end{array}
 }
\ExplSyntaxOff

\begin{document}

\begin{table}
\caption{Recursive definition of a formula} \label{fig:formula}

\begin{syntax}
\syntaxitem[,]{\ent{Formula}} % use comma just by way of example
 {
  \ent{PrimitiveFormula},
  (\ent{Formula} \mathbin{\ent{Connective}} \ent{Formula}),
  \lnot \ent{Sentence},
  \ent{Quantifier} \, \ent{Variable} \, \ent{Formula}
 }
\\[2ex]
\syntaxitem{PrimitiveFormula}
 {
  \ent{Predicate}(\ent{Term},\dots,\ent{Term})
 }
\\[2ex]
\syntaxitem{\ent{Term}}
 {
  \ent{Function}(\ent{Term},\dots,\ent{Term});
  \ent{Constant};
  \ent{Variable}
 }
\end{syntax}
\end{table}

A cross-reference to table \ref{fig:formula}.

\end{document}

enter image description here

1

How to do this by \halign in plain TeX:

\input opmac
\def\rmbrackets{\adef({{\rm(}}\adef){{\rm)}}}\rmbrackets

\hfil\vbox{
\halign{\hfil\it#\unskip\ &\hfil$#$\hfil&\ \it#\hfil\cr
  Formula &\to& Primitive Formula\cr
          &|&   (Formula Connective Formula)\cr
          &|&   $\neg$ Sentence\cr
          &|&   Quantifier Variable Formula\cr
                                            \noalign{\medskip}
  Primitive Formula &\to& Predicate(Term,\dots,Term)\cr
                                                    \noalign{\medskip}
  Term    &\to& Function(Term,\dots,Term)\cr
          &|&   Constant\cr
          &|&   Variable\cr
}}

\bye

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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