adjustbox 和 lstnewenvironment

adjustbox 和 lstnewenvironment

我正在尝试使用它adjustbox来围绕列表制作一个框架(我知道它listings本身可以做到这一点,但是当整个代码中的行高不相等时,它的实现会产生次优结果)。这是我尝试过的:

\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}

我尝试定义一个新的 listings 环境,用于adjustbox在列表周围放置一个框架。但是,这无法编译,只是在运行时挂起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')

在 Texmaker 中,pdflatex情况更加复杂:“紧急停止。<> test.tex(作业中止,未找到合法的 \end)”

答案1

\adjustbox{<options}{<box contents>}您可以使用手册中所述的宏版本,该版本也适用于\bgroup\egroup: \adjustbox{<options}\bgroup<box contents>\egroup。这允许以下定义:

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

在此处输入图片描述

\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}

相关内容