3

I'm trying to use adjustbox to make a frame around listings (I know that listings itself can do this, but its implementation produces sub-optimal results when the line heights are not equal throughout the code). Here is what I tried:

\documentclass{article}
\usepackage{listings}
\usepackage{adjustbox}
% sanity checking: the following line works
%\lstnewenvironment{listing}{}{}

% the following line is what I'd like to do
\lstnewenvironment{listing}{\begin{adjustbox}{left,trim=0 0 0 2pt,fbox}}{\end{adjustbox}}

\begin{document}
\begin{listing}
this is 
some
test
code
\end{listing}
\end{document}

I tried defining a new listings environment that uses adjustbox to put a frame around the listing. However, this does not compile, but just hangs on running pdflatex:

*         % I just pressed enter here
(Please type a command or say `\end')
*\end     % typing \end does not work either ;)

*\end

*
(Please type a command or say `\end')

In Texmaker, pdflatex is a bit more chatty: "Emergency stop. <> test.tex (job aborted, no legal \end found)"

1 Answer 1

4

You can use the macro version \adjustbox{<options}{<box contents>} which as the manual states also works with \bgroup and \egroup: \adjustbox{<options}\bgroup<box contents>\egroup. That allows the following definition:

\lstnewenvironment{listing}
  {\adjustbox{left,trim=0 0 0 2pt,fbox}\bgroup}
  {\egroup}

enter image description here

\documentclass{article}
\usepackage{listings}
\usepackage{adjustbox}

% the following line is what I'd like to do
\lstnewenvironment{listing}
  {\adjustbox{left,trim=0 0 0 2pt,fbox}\bgroup}
  {\egroup}

\begin{document}
\begin{listing}
this is 
some
test
code
\end{listing}
\end{document}

You must log in to answer this question.

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