0

I like to define the following command :

\newcommand{\page}[2]{\begin{minipage}{#1\textwidth} #2 \end{minipage}}

So that I can do stuff like

\page{0.1}{blabla}\page{0.3}{lalala}

I've recently discovered the package listings and I've noticed a very strange interaction.

This code doesn't trigger any error :

\begin{minipage}{0.5\textwidth}
\begin{lstlisting}  
    bla bla
\end{lstlisting}
\end{minipage}

But this :

\page{0.5}{
\begin{lstlisting}  
    bla bla
\end{lstlisting}
}

throws : Emergency stop. [...] job aborted, no legal \end found

And I must say I'm very confused, since I do not see much difference between those two.

8
  • 1
    You cannot use the lstlisting environment in the argument to another command. I don't think you gain so much with \page rather than the full environment form.
    – egreg
    Mar 12, 2018 at 18:07
  • @egreg Oh well that explains it! You just can't ? Isn't there any way to do so ?
    – krirkrirk
    Mar 12, 2018 at 18:10
  • @egreg about \page, I use a lot of minipages in my documents for formating, and I can assure you that the gain of time is huge.
    – krirkrirk
    Mar 12, 2018 at 18:11
  • 1
    you can not use any verbatim (or verbatim-like command) inside the argument of another command, but more generally hiding environment syntax has several disadvantages, making it harder for editors to give correct syntax highlighting and context sensitive help Mar 12, 2018 at 18:26
  • @DavidCarlisle Ok, thank you. I didn't know that was a bad habit, I actually do that for pretty much any environment - enumerate, minipage with fbox... I'm sorry for editors but this makes writing so much more comfortable!
    – krirkrirk
    Mar 12, 2018 at 18:34

1 Answer 1

1

If you define our \page macro using directly primitive commands (\vtop or \vbox), then you will not have problem with verbatim environment inside \page parameter:

\documentclass{article}

\def\page#1{\vtop\bgroup \hsize=#1\hsize \let\next}

\begin{document}

\page{0.5}{
\begin{verbatim}
bla bla
\end{verbatim}
}

\end{document}
2
  • I'm not sure what's \def\page#1{\vtop\bgroup \hsize=#1\hsize \let\next} doing, but it indeed allows a lstlisting argument. Yet, it does not seem to behave like minipage. In particular, each page is printed on a new line, while pages from minipage can be put on a same line, which is perfect for layout.
    – krirkrirk
    Mar 15, 2018 at 17:30
  • @krirkrirk You can insert \leavevmode just before \vtop, it means \def\page#1{\leavevmode\vtop\bgroup.... Then each \page behaves as one "big" character, i. e. it starts horizontal mode (like minipage).
    – wipet
    Mar 15, 2018 at 18:26

You must log in to answer this question.

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