I am trying to use a lstlisting
environment inside a minipage
, inside an align*
(fromo amsmath
)... However, lualatex
throws an error. This is the minimum working examp
\documentclass{article}
\usepackage{amsmath}
\usepackage{listingsutf8}
\begin{document}
\begin{align*}
f(a\in A) &= x\\
f(x\in X) &= \begin{minipage}[t]{4cm}
\begin{lstlisting}[mathescape=true]
(let $m$=mean($X$)
$g(m) \circ h(x)$)
\end{lstlisting}
\end{minipage}
\end{align*}
\end{document}
The error is below:
! Argument of \lst@next has an extra }.
<inserted text>
\par
l.16 \end{align*}
?
! Emergency stop.
<inserted text>
\par
l.16 \end{align*}
So, is there any way to do what I want? I'd like to define a function by cases, using align*
, but with indented code on the right side
I'm not sure if it helps to mention that I'm typesetting the denotational semantics of a language, using some Lisp-like syntax on the right side of the equations defining semantic functions. A pseudo-example, formatted as plain text, follows.
C[ v := e ]s = s[ E[e]/v ]
C[ some-command ]s = (let x=...
(if ...
then ...
else ...))
edit: As suggested by egreg, I tried the tabbing
environment:
\documentclass{article}
\usepackage{amsmath}
\usepackage{listingsutf8}
\lstset{%
showstringspaces=false,
basicstyle=\ttfamily,
commentstyle=\fontfamily{cmtt}\fontshape{it}\selectfont,
frame=single
}
\begin{document}
\begin{center}
\begin{tabbing}
$f(a\in A)$ = \= $x$\\
$f(x\in X)$ = \> (let $m$=mean($X$) in\\
\> $\;\;\;\;$ \= $g(m) \circ h(x)$)
\end{tabbing}
\end{center}
\begin{center}
\begin{tabbing}
$f(a\in A)$ = \= $x$\\
$f(x\in X)$ = \> \begin{minipage}[t]{4cm}
\begin{lstlisting}[mathescape=true]
(let $m$=mean($X$) in:
$g(m) \circ h(x)$)
\end{lstlisting}
\end{minipage}
\end{tabbing}
\end{center}
\end{document}
This is the result:
There are two attempts in the document. The first seems cumbersone to typeset (and controlling indentation would be somewhat painful); the second is easier to type, but the first line of the lstlisting
inside the minipage
isn't aligned with the equation (I've included the frame in the lstlisting
environment only to make the alignment clear).
Also, the equal signs don't seem to be aligned in both cases.
Is this easy to fix?
lstlisting
, in the argument to another command;align*
absorbs the environment's content as the argument to a command. But do you really needlstlisting
for that? – egreg Feb 27 '16 at 12:11lstlisting
, but I need the code on the right side of the equation to be properly aligned -- and I'll be mixing texttt and math mode there.lstlisting
makes that easy, but I'll use anything else that works! – Jay Feb 27 '16 at 12:15tabbing
; surely withalign
you can not “obey spaces and newlines”, because of how it works. – egreg Feb 27 '16 at 12:17tabbing
, maybe those are easy to fix? – Jay Feb 27 '16 at 13:46