lstlisting 环境的别名

lstlisting 环境的别名

我在实验室笔记本中使用 listings 包来编写代码和包含电子邮件副本。它运行良好。

但因为我永远记不住我用于电子邮件的选项(主要是为了让我能直接粘贴而不更改某些字符),所以我想创建一个新的环境,即一个别名。我似乎无法让它工作。

下面是一个 MWE。如果我取消注释\begin{lstlisting}[inputencoding=latin1, style=email]和相关的\end{lstlisting},并注释\begin{Email}和它的\end,它就可以正常工作。如果我保持原样,我会得到

!LaTeX 错误:\begin{Email} 在输入第 96 行以 \end{document} 结束。

这对我来说没什么意义。显然我的 \newenvironment 命令有问题,但我无论如何也想不出是什么问题!有什么建议吗?

梅威瑟:

 \documentclass[nobib, sfsidenotes]{tufte-book}
%%%%%%%%%
% color stuff %
\usepackage[dvipsnames]{xcolor}
% COLORS (Tango) Mostly by Philip Bunge
% http://pbunge.crimson.ch/
\definecolor{tenPercentGrey}{gray}{0.9}
\definecolor{White}{gray}{0.9}
\definecolor{Black}{gray}{0.0}
\definecolor{LightButter}{rgb}{0.98,0.91,0.31}
\definecolor{LightOrange}{rgb}{0.98,0.68,0.24}
\definecolor{LightChocolate}{rgb}{0.91,0.72,0.43}
\definecolor{LightChameleon}{rgb}{0.54,0.88,0.20}
\definecolor{LightSkyBlue}{rgb}{0.45,0.62,0.81}
\definecolor{LightPlum}{rgb}{0.68,0.50,0.66}
\definecolor{LightScarletRed}{rgb}{0.93,0.16,0.16}
\definecolor{Butter}{rgb}{0.93,0.86,0.25}
\definecolor{Orange}{rgb}{0.96,0.47,0.00}
\definecolor{Chocolate}{rgb}{0.75,0.49,0.07}
\definecolor{Chameleon}{rgb}{0.45,0.82,0.09}
\definecolor{SkyBlue}{rgb}{0.20,0.39,0.64}
\definecolor{Plum}{rgb}{0.46,0.31,0.48}
\definecolor{ScarletRed}{rgb}{0.80,0.00,0.00}
\definecolor{DarkButter}{rgb}{0.77,0.62,0.00}
\definecolor{DarkOrange}{rgb}{0.80,0.36,0.00}
\definecolor{DarkChocolate}{rgb}{0.56,0.35,0.01}
\definecolor{DarkChameleon}{rgb}{0.30,0.60,0.02}
\definecolor{DarkSkyBlue}{rgb}{0.12,0.29,0.53}
\definecolor{DarkPlum}{rgb}{0.36,0.21,0.40}
\definecolor{DarkScarletRed}{rgb}{0.64,0.00,0.00}
\definecolor{Aluminium1}{rgb}{0.93,0.93,0.92}
\definecolor{Aluminium2}{rgb}{0.82,0.84,0.81}
\definecolor{Aluminium3}{rgb}{0.73,0.74,0.71}
\definecolor{Aluminium4}{rgb}{0.53,0.54,0.52}
\definecolor{Aluminium5}{rgb}{0.33,0.34,0.32}
\definecolor{Aluminium6}{rgb}{0.18,0.20,0.21}

%%% LISTINGS
\usepackage[]{listings}
\lstset{
  backgroundcolor=\color{tenPercentGrey}, %
  basicstyle=\color{Black}\ttfamily{}, %
  keywordstyle=[1]\color{DarkSkyBlue}, %
  keywordstyle=[2]\color{DarkScarletRed}, %
  keywordstyle=[3]\bfseries{}, %
  keywordstyle=[4]\color{DarkPlum}, %
  keywordstyle=[5]\color{SkyBlue}, %
  commentstyle=\color{Aluminium4}, %
  stringstyle=\color{Chocolate}, %
  identifierstyle=\color{Black}, %
  emphstyle=\color{Black}, %
  numbers=left, %
  stepnumber=1, %
  frame=tb, %
  captionpos=b, %
  lineskip=\smallskipamount{}, %
  aboveskip=\bigskipamount{}, %
  belowskip=\medskipamount{}, %
  commentstyle=\itshape\small{}, %
  tabsize=2, %
  breaklines=true, %
  postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}, %
  rulecolor=\color{Black!30}, %
  showspaces=false, %
  showstringspaces=false, %
  showtabs=false, %
  breaklines=TRUE,
  language=R
 }
%
\lstdefinestyle{email}
  {
  stringstyle=\color{Black}, %
  texcl=true,
  postbreak=,
  literate={’}{'}{1}
           {“}{``}{1}
           {”}{''}{1}
           {•}{- }{1}
           {—}{--}{1}
           {–}{--}{1},
  language=R
 }
%
%
%%%%%%%%
\newenvironment{Email}{
     \begin{lstlisting}[inputencoding=latin1, style=email]
    }    {
     \end{lstlisting}
    }
%%%%%%%%%%%%%%
\begin{document}
%
%\begin{lstlisting}[inputencoding=latin1, style=email]
\begin{Email}
Test email text
\end{Email}
%\end{lstlisting}
%
\end{document} 

答案1

嵌套逐字内容并不容易,尤其是对于这种情况listings提供\lstnewenvironment以便使用户能够应用自定义的listings环境。

的语法\lstnewenvironment与 相同,\newenviroment但正确地包裹了底层listings机制。可选参数可用于\lstset,其中某些默认值(例如)style=email已在环境启动代码中指定。

另一个可能的答案将采用一些奇怪\scantokens等的方法或提供的非常好的列表环境tcolorbox

 \documentclass[nobib, sfsidenotes]{tufte-book}
%%%%%%%%%
% color stuff %
\usepackage[dvipsnames]{xcolor}
% COLORS (Tango) Mostly by Philip Bunge
% http://pbunge.crimson.ch/
\definecolor{tenPercentGrey}{gray}{0.9}
\definecolor{White}{gray}{0.9}
\definecolor{Black}{gray}{0.0}
\definecolor{LightButter}{rgb}{0.98,0.91,0.31}
\definecolor{LightOrange}{rgb}{0.98,0.68,0.24}
\definecolor{LightChocolate}{rgb}{0.91,0.72,0.43}
\definecolor{LightChameleon}{rgb}{0.54,0.88,0.20}
\definecolor{LightSkyBlue}{rgb}{0.45,0.62,0.81}
\definecolor{LightPlum}{rgb}{0.68,0.50,0.66}
\definecolor{LightScarletRed}{rgb}{0.93,0.16,0.16}
\definecolor{Butter}{rgb}{0.93,0.86,0.25}
\definecolor{Orange}{rgb}{0.96,0.47,0.00}
\definecolor{Chocolate}{rgb}{0.75,0.49,0.07}
\definecolor{Chameleon}{rgb}{0.45,0.82,0.09}
\definecolor{SkyBlue}{rgb}{0.20,0.39,0.64}
\definecolor{Plum}{rgb}{0.46,0.31,0.48}
\definecolor{ScarletRed}{rgb}{0.80,0.00,0.00}
\definecolor{DarkButter}{rgb}{0.77,0.62,0.00}
\definecolor{DarkOrange}{rgb}{0.80,0.36,0.00}
\definecolor{DarkChocolate}{rgb}{0.56,0.35,0.01}
\definecolor{DarkChameleon}{rgb}{0.30,0.60,0.02}
\definecolor{DarkSkyBlue}{rgb}{0.12,0.29,0.53}
\definecolor{DarkPlum}{rgb}{0.36,0.21,0.40}
\definecolor{DarkScarletRed}{rgb}{0.64,0.00,0.00}
\definecolor{Aluminium1}{rgb}{0.93,0.93,0.92}
\definecolor{Aluminium2}{rgb}{0.82,0.84,0.81}
\definecolor{Aluminium3}{rgb}{0.73,0.74,0.71}
\definecolor{Aluminium4}{rgb}{0.53,0.54,0.52}
\definecolor{Aluminium5}{rgb}{0.33,0.34,0.32}
\definecolor{Aluminium6}{rgb}{0.18,0.20,0.21}

%%% LISTINGS
\usepackage[]{listings}
\lstset{
  backgroundcolor=\color{tenPercentGrey}, %
  basicstyle=\color{Black}\ttfamily{}, %
  keywordstyle=[1]\color{DarkSkyBlue}, %
  keywordstyle=[2]\color{DarkScarletRed}, %
  keywordstyle=[3]\bfseries{}, %
  keywordstyle=[4]\color{DarkPlum}, %
  keywordstyle=[5]\color{SkyBlue}, %
  commentstyle=\color{Aluminium4}, %
  stringstyle=\color{Chocolate}, %
  identifierstyle=\color{Black}, %
  emphstyle=\color{Black}, %
  numbers=left, %
  stepnumber=1, %
  frame=tb, %
  captionpos=b, %
  lineskip=\smallskipamount{}, %
  aboveskip=\bigskipamount{}, %
  belowskip=\medskipamount{}, %
  commentstyle=\itshape\small{}, %
  tabsize=2, %
  breaklines=true, %
  postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}}, %
  rulecolor=\color{Black!30}, %
  showspaces=false, %
  showstringspaces=false, %
  showtabs=false, %
  breaklines=TRUE,
  language=R
 }
%
\lstdefinestyle{email}
  {
  stringstyle=\color{Black}, %
  texcl=true,
  postbreak=,
  literate={’}{'}{1}
           {“}{``}{1}
           {”}{''}{1}
           {•}{- }{1}
           {—}{--}{1}
           {–}{--}{1},
  language=R
 }
%
%
%%%%%%%%
 \lstnewenvironment{Email}[1][]{\lstset{inputencoding=latin1, style=email,#1}%
 }{%
 }
%%%%%%%%%%%%%%
\begin{document}
%
%\begin{lstlisting}[inputencoding=latin1, style=email]
\begin{Email}
Test email text
\end{Email}
%\end{lstlisting}
%
\end{document} 

在此处输入图片描述

相关内容