4

I'm using the listings package in my lab notebook both for code and for including copies of emails. It works fine.

But because I can never remember the options I use for emails (mainly to allow me to paste them in directly without changing some characters), I'd like to create a new environment that's an alias. I can't seem to get it to work.

An MWE is below. If I uncomment the \begin{lstlisting}[inputencoding=latin1, style=email] and the associated \end{lstlisting}, and comment the \begin{Email} and its \end, it works fine. If I leave it as is, I get

! LaTeX Error: \begin{Email} on input line 96 ended by \end{document}.

which doesn't make a lot of sense to me. Obviously something's wrong with my \newenvironment command, but for the life of me I can't figure out what it is! Any suggestions?

MWE:

 \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
    You should use \lstnewenvironment in order to grab the verbatim content correctly – user31729 Dec 12 '17 at 18:27
3

Nesting verbatim content is not easy, especially for this case listings provides\lstnewenvironment in order to enable users to apply self-defined listings - like environments.

The syntax of \lstnewenvironment is equal to \newenviroment but wraps correctly around the underlying listings mechanism. The optional argument can be used for \lstset, where some default values such as style=email have been specified in the environment startup code already.

Another possible answer will apply some weird \scantokens etc. approach or the very nice listing environments provided by 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} 

enter image description here

  • @gfox: You're welcome. Happy TeXing – user31729 Dec 13 '17 at 20:05

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.