1

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:

enter image description here

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?

4
  • 3
    You can't use verbatim environments, including lstlisting, in the argument to another command; align* absorbs the environment's content as the argument to a command. But do you really need lstlisting for that? – egreg Feb 27 '16 at 12:11
  • I don't need lstlisting, 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:15
  • 2
    I can think to tabbing; surely with align you can not “obey spaces and newlines”, because of how it works. – egreg Feb 27 '16 at 12:17
  • @egreg: see the edit... There are a few issues when I use tabbing, maybe those are easy to fix? – Jay Feb 27 '16 at 13:46
2

You can't use lstlisting in the body of an align* environment, because it's the same as using it in the argument to a command, which is not allowed.

I suggest tabbing:

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\begin{align*}
f(a\in A) &= x\\
f(x\in X) &=
  \begin{minipage}[t]{\textwidth}\ttfamily
  \begin{tabbing}
    (let \= $m=\texttt{mean}(X)$ in\\
         \> $g(m) \circ h(x)$)
  \end{tabbing}
  \end{minipage}
\end{align*}

\end{document}

This takes advantage from the fact that a minipage that only encloses a tabbing environment is as wide as the widest line in the tabbing.

enter image description here

2
  • Perfect! And it seems, as I just found out, that there is a very clean way to control identation (see this answer ) – Jay Feb 28 '16 at 22:36
  • 1
    @Jay Basically tabbing is thought for such applications. Search the site for other tricks with it. – egreg Feb 28 '16 at 22:41

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.