1

As I need to pass a verbatim environment into a command, I use \savebox and then render it inline with the rest of the text.

\documentclass{article}

\usepackage{listings}
\usepackage{enumitem}
\usepackage{mdframed}

\setlength{\textwidth}{500pt}
\setlength{\oddsidemargin}{-20pt}
\setlength{\evensidemargin}{-20pt}

% Define questions environment with framed \question
\newcommand{\questionlabel}{Question }
\newcommand{\restorequestionlabel}{\renewcommand{\questionlabel}{Question }}
\newcommand{\setquestionlabel}[1]{\renewcommand{\questionlabel}{#1 }}
\newlist{questions}{enumerate}{4}
\setlist[questions]{align=left,ref=\questionlabel\arabic*,label=\textbf{\questionlabel\arabic*.},wide}
\newcommand{\itembr}{\needspace{10\baselineskip}\item\mbox{}}
\newcommand{\question}[1]{\itembr\begin{mdframed}#1\end{mdframed}}

\begin{document}

\begin{questions}

\begin{lrbox}{0}
\begin{lstlisting}[language=C]
int* p = (int*) malloc(100 * sizeof(int));
\end{lstlisting}
\end{lrbox}
\question{
    Explain what \usebox{0} does.
}

\end{questions}

\end{document}

The \question command is used to put the text into an mdframed. Since it's declared using \newcommand, there is no way to pass in any verbatim environment directly. Using verbatimbox is not an option as I need syntax highlighting from listings.

The problem is that the resulting text seems to be vertically aligned at the top instead of bottom:

Different bottom lines

Can I somehow align everything automatically? I know \raisebox exists, but I'd need to get to know the height difference somehow.

  • Please tell us how the \question command is defined, because without it there is not problem. – Andrew Swann Oct 23 '17 at 14:34
  • 1
    you probably want lstinline rather than lstlisting – David Carlisle Oct 23 '17 at 15:03
  • @DavidCarlisle I want the font that lstlisting uses, rather than computer modern with few words in bold – zlotnleo Oct 23 '17 at 15:06
  • lstinline uses the same formatting as lstlisting, but for a one-line inline use – David Carlisle Oct 23 '17 at 15:10
  • Any news here? Did you try \lstinline? – TeXnician Jan 4 '18 at 21:23
1

I can't reproduce your image. However, boxpos=t should be what you want:

\documentclass{article}

\usepackage{listings}

\lstset{boxpos=t}

\begin{document}

\begin{lrbox}{0}
\begin{lstlisting}[language=C]
int* p = (int*) malloc(100 * sizeof(int));
\end{lstlisting}
\end{lrbox}

Explain what \usebox{0}; does.

\begin{lrbox}{0}
\begin{lstlisting}[language=C]
SWAP(t, x, y)
\end{lstlisting}
\end{lrbox}

Define a macro SWAP\usebox{0} that exchanges two arguments of type t.

\end{document}

I added text to emphasize the correctness of the output.

enter image description here

On the other hand, \lstinline seems much easier.

\documentclass{article}

\usepackage{listings}

\begin{document}

Explain what \lstinline[language=C]{int* p = (int*) malloc(100 * sizeof(int));}
does.

Define a macro \lstinline[language=C]{SWAP(t, x, y)}
that exchanges two arguments of type t.

\end{document}

enter image description here

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.