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

Formula $\to$ PrimitiveFormula $\vert$ (Formula Connective Formula)
$\vert \neg$ Sentence $\vert$ Quantifier Variable Formula
Primitive Formula $\to$ Predicate(Term,\cdots ,Term)

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

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

\caption{A formula} \label{fig:formula}
\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 \\

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

You can use align* for this.


\newcommand{\ent}[1]{\mathit{#1}} % `entity'


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

\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}

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

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).




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

 { % #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 & }


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

\syntaxitem[,]{\ent{Formula}} % use comma just by way of example
  (\ent{Formula} \mathbin{\ent{Connective}} \ent{Formula}),
  \lnot \ent{Sentence},
  \ent{Quantifier} \, \ent{Variable} \, \ent{Formula}

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


enter image description here


How to do this by \halign in plain TeX:

\input opmac

\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
  Primitive Formula &\to& Predicate(Term,\dots,Term)\cr
  Term    &\to& Function(Term,\dots,Term)\cr
          &|&   Constant\cr
          &|&   Variable\cr


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.