8

I'm writing some notes on parsing expressions, and one thing I want to illustrate is prefix notation equations. With this in mind, I want to be able to indent certain pieces of an s-expression. For instance, here's what I'm looking for:

(−
  (+
    (∗ 7 −9)
    (−6
      (/ −3 2))))

However, I can't figure out an easy way to do this. Ideally, these expressions would also be centered and have the ability to "unindent" some subexpressions.

0

5 Answers 5

1

Maybe the use of minted is the good choice here. With the code below, you will obtain the following output.

enter image description here

\documentclass{article}
\usepackage{minted}

\begin{document}

\begin{minted}{lisp}
(-
  (+
    (* 7 -9)
    (-6
      (/ -3 2))))
\end{minted}

\end{document}
1
  • 1
    I didn't think about using minted! That's a great idea. Commented Aug 26, 2022 at 1:19
8

enter image description here

This obeys newlines but ignores white space re-indenting acording to () nesting depth. * and − can be entered as ascii * and - or Unicode ∗ − as in the question.

The whole expression is centred on its longest line

\documentclass{article}

\DeclareUnicodeCharacter{2212}{\ensuremath{-}}
\DeclareUnicodeCharacter{2217}{\ensuremath{*}}
\newcount\sexpdepth
\catcode`\(\active
\catcode`\)\active
\catcode`\-\active
\catcode`\*\active
\newenvironment{sexp}
{\center
\global\sexpdepth=0 %
\bgroup
\catcode`\(\active
\catcode`\)\active
\catcode`\-\active
\catcode`\*\active
\edef-{$\string-$}%
\edef*{$\string*$}%
\edef({\string(\global\advance\sexpdepth1 }%
\edef){\string)\global\advance\sexpdepth\string-1 }%
\def\obeyedline{\\\hspace{\sexpdepth em}}%
\obeylines
\begin{tabular}{@{}l@{}}}
{\end{tabular}\egroup
\endcenter}
\catcode`\(12
\catcode`\)12
\catcode`\-12
\catcode`\*12

\begin{document}

\begin{sexp}
  (+
1
2
3)
\end{sexp}

\begin{sexp}
(−
  (+
  (∗ 7 −9)
  (−6
      (/ 1 −32))
          (* 1 2)
    5))  
\end{sexp}

\end{document}
8

An option would be to use the tabbing environment (inside a minipage and center environment for centering the construction)

\begin{center}
\begin{minipage}{.5\textwidth}
\begin{tabbing}
    $(-$\=\+\\
      $(+$\=\+\\
         $(\times$ $7$ $-9$\\
         $(-$\=\ $6$\+\\
             $(/$ $-3$ $2)))))$
\end{tabbing}   
\end{minipage}  
\end{center}    

enter image description here

In the tabbing environment \= sets a tab stop, \+ move the first stop in the next lines to the “next” tab stop.

1
  • 3
    you'd want {-} to avoid the infix spacing on - (* 7 -9) is "7 times -9 " not "star 7 minus 9" and \= before 6 not after as the arguments of (- are 6 and (/ -3 2) so should be indended the same amount Commented Aug 20, 2022 at 15:42
2

I would suggest using an ordinary verbatim environment.

Sample verbatim

\documentclass{article}

\begin{document}

\begin{verbatim}
(-
  (+
    (* 7 -9)
    (-6
      (/ -3 2))))
\end{verbatim}

\end{document}

Or you can treat this as programming code and use either listings or minted. Here is a minted example, treating this as lisp like material. See the documentation of minted to see how to set the package up; you will need to remember to compile with -shell-escape.

Sample lisp

\documentclass{article}

\usepackage{minted}

\begin{document}

\begin{minted}{cl}
(-
  (+
    (* 7 -9)
    (-6
      (/ -3 2))))
\end{minted}

\end{document}
1
\documentclass[12pt]{report}
\begin{document}
\noindent (-

(+

\indent\indent $(\times 7 - 9)$

\indent\indent (-6

\indent\indent\indent (/ - 32))))
\end{document}

enter image description here

To achieve this multi-indent you need to use \indent multiple times.

1
  • 2
    eek why a (infix!!) minus before 9 but hyphens on the other lines, why no space after * why are the arguments of - indented by different amounts? Commented Aug 20, 2022 at 9:03

You must log in to answer this question.

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