两列文本内的全宽代码列表

两列文本内的全宽代码列表

我用它multicols来创建两列正文。对于代码排版,我使用listings包。对于后者,我使用\lstnewenvironment命令创建了一个环境,用作\begin{Code}..\end{Code}。我希望将代码排版为全屏宽度

\begin{multicols}{2}
  ... two column text
  \begin{Code}...\end{Code}
  ... two column text
\end{multicols}

我所追求的是以Code某种方式重新定义环境,以便multicols在开始排版之前结束,然后在最后重新启动它。

\documentclass{article}
\usepackage{listings,lipsum,xcolor}
\usepackage{multicol}
\renewcommand{\ttdefault}{cmtt}         % prefer old tt font
\newcommand\emphasis[2][red]{\lstset{emph={write,void,writeln,#2},
   emphstyle={\ttfamily\textcolor{#1}}}}%
\lstnewenvironment{Code}[1][]
  {\lstset{language=[LaTeX]TeX}\lstset{%
      escapeinside={{(*@}{@*)}},
      breaklines=true,
      framesep=5pt,
      basicstyle=\ttfamily,
      showstringspaces=false,
      keywordstyle=\ttfamily\textcolor{blue},
      stringstyle=\color{orange},
     commentstyle=\color{black},
     rulecolor=\color{gray!10},
      breakatwhitespace=true,
     showspaces=false,  % shows spacing symbol
     xleftmargin=0pt,
     xrightmargin=5pt,
     aboveskip=0pt, % compact the code looks ugly in type
     belowskip=0pt,  % user responsible to insert any skips
      backgroundcolor=\color{gray!15}, #1
}}
{}
\begin{document}
\begin{multicols}{2}
\lipsum[1]
\begin{Code}
  \def\somelongcommand{This is a test to demonstrate the problem}
\end{Code}
\lipsum[3]
\end{multicols}

Preferably,

\begin{Code}
  \def\somelongcommand{This is a test to demonstrate the problem}
\end{Code}

\end{document}

答案1

您可以使用带有特殊挂钩的 etoolbox 包。

\documentclass{article}
\usepackage{listings,lipsum,xcolor}
\usepackage{multicol}
\renewcommand{\ttdefault}{cmtt}         % prefer old tt font
\newcommand\emphasis[2][red]{\lstset{emph={write,void,writeln,#2},
   emphstyle={\ttfamily\textcolor{#1}}}}%
\lstnewenvironment{Code}[1][]
  {\lstset{language=[LaTeX]TeX}\lstset{%
      escapeinside={{(*@}{@*)}},
      breaklines=true,
      framesep=5pt,
      basicstyle=\ttfamily,
      showstringspaces=false,
      keywordstyle=\ttfamily\textcolor{blue},
      stringstyle=\color{orange},
     commentstyle=\color{black},
     rulecolor=\color{gray!10},
      breakatwhitespace=true,
     showspaces=false,  % shows spacing symbol
     xleftmargin=0pt,
     xrightmargin=5pt,
     aboveskip=0pt, % compact the code looks ugly in type
     belowskip=0pt,  % user responsible to insert any skips
      backgroundcolor=\color{gray!15}, #1
}}
{}
\usepackage{etoolbox}
\makeatletter
\newtoggle{test:multicols}
\newcounter{savecolnum}
\AtBeginEnvironment{multicols}{%
   \global\toggletrue{test:multicols}
   }
\AtEndEnvironment{multicols}{%
 \global\setcounter{savecolnum}{\number\col@number}
 \global\togglefalse{test:multicols}
}


\BeforeBeginEnvironment{Code}{
   \iftoggle{test:multicols}%
      {\def\reserveda{\end{multicols}\global\toggletrue{test:multicols}}}
      {\let\reserveda\relax}
   \reserveda
}
\AfterEndEnvironment{Code}{%
   \iftoggle{test:multicols}%
      {\def\reserveda{\begin{multicols}{\thesavecolnum}}}
      {\let\reserveda\relax}
   \reserveda
}
\makeatother
\begin{document}
\begin{multicols}{2}
\lipsum[1]
\begin{Code}
  \def\somelongcommand{This is a test to demonstrate the problem}
\end{Code}
\lipsum[3]
\end{multicols}

Preferably,

\begin{Code}
  \def\somelongcommand{This is a test to demonstrate the problem}
\end{Code}

\end{document}

结果:

答案2

一揽子计划etoolbox来救场:我们必须评估当前的环境 \begin{Code}已完全处理:

\usepackage{etoolbox}
\makeatletter
\def\multicols@string{multicols}
\BeforeBeginEnvironment{Code}{%
  \ifx\@currenvir\multicols@string
    \xdef\resume@multicols{\noexpand\begin{multicols}{\number\col@number}}%
    \end{multicols}%
  \else
    \global\let\resume@multicols\@empty
  \fi}
\AfterEndEnvironment{Code}{\resume@multicols}
\makeatother

当然,你必须确保不要\begin{Code}在另一个环境中发出警告。这种测试可以在\BeforeBeginEnvironment代码内部实现,通过发出警告。

相关内容