6

I'm working on a document using the tufte-handout class in which I need to include some examples of LaTeX code for which I try to work with the package showexpl in the way it appears in my MWE:

\documentclass[twoside]{tufte-handout}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage[spanish,mexico]{babel}    
\usepackage{showexpl}

 \lstloadlanguages{[LaTeX]Tex}
 \lstset{%
     basicstyle=\ttfamily\small,
     commentstyle=\itshape\ttfamily\small,
     showspaces=false,
     showstringspaces=false,
     breaklines=true,
     breakautoindent=true,
     captionpos=t
 }

\begin{document}
\begin{LTXexample}  
\begin{itemize}
\item Introducción a \LaTeX
\item Texto con \LaTeX
\begin{itemize}
\item Reglas básicas
\item Entornos de texto
\begin{itemize}
\item Incisos, citaciones y poemas
\begin{itemize}
\item \textsf{quote}
\item \textsf{quotation}
\item \textsf{verse}
\end{itemize}
\item Listas
\begin{itemize}
\item \textsf{itemize}
\item \textsf{enumerate}
\item \textsf{description}
\end{itemize}
\item Texto alineado horizontalmente
\begin{itemize}
\item \textsf{flushleft}
\item \textsf{center}
\item \textsf{flushright}
\end{itemize}
\item Texto mecanografiado
\begin{itemize}
\item \textsf{verbatim}
\end{itemize}
\end{itemize}
\end{itemize}
\end{itemize}
\end{LTXexample}
\end{document}

However, something goes wrong, and seems to be unable to compile. I don't know if it has to see the width of the typographic case in this class or if it's because it can't works in two columns.

I receive the following error message:

Package inputenc Error: Unicode char \u8:\expandafter not set up for use with LaTeX.
Package inputenc Error: Keyboard character used is undefined

It only happens wihen I try to introduce a LTXexampeenvironment. Otherwise the compilation runs well.

5

The problem is not the document class, but the accented letters from Spanish (in this particular case, "ó" and "á"). The known workarounds for listings: using literate or even escaping the accented characters to LaTeX (suugested, for example, in How to insert code with accents with listings?) won't work for showexpl since non-ascii characters get lost when the temporary file internally used is written.

I'd like to suggest you the powerful tcolorbox package and its nice interaction with listings; here's a little example (adjust the settings according to your needs; in particular, you need to add support for accented uppercase vowels, the crema and the eñe):

\documentclass[twoside]{tufte-handout}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish,mexico]{babel}    
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

 \lstset{%
     language=[LaTeX]TeX,
     basicstyle=\ttfamily\small,
     commentstyle=\itshape\ttfamily\small,
     showspaces=false,
     showstringspaces=false,
     breaklines=true,
     breakautoindent=true,
     captionpos=t,
     literate=%
       {á}{{\'a}}1 
       {é}{{\'e}}1 
       {í}{{\'i}}1 
       {ó}{{\'o}}1 
       {ú}{{\'u}}1 
}

\tcbset{
  example/.style 2 args={
    colframe=red!50!black, 
    colback=red!50!black!5,
    fonttitle=\small\sffamily\bfseries, 
    fontupper=\small,   
    fontlower=\small,
    text outside listing,
    title={Ejemplo~\thetcbcounter: #1},label={#2}},
}
\newtcblisting[auto counter]{micodigo}[3][]{example={#2}{#3},#1}

\begin{document}

\begin{micodigo}{ejemplo de listas}{eje:lista}
\begin{itemize}
\item Introducción a \LaTeX
\item Texto con \LaTeX
\begin{itemize}
\item Reglas básicas
\item Entornos de texto
\begin{itemize}
\item Incisos, citaciones y poemas
\begin{itemize}
\item \textsf{quote}
\item \textsf{quotation}
\item \textsf{verse}
\end{itemize}
\item Listas
\begin{itemize}
\item \textsf{itemize}
\item \textsf{enumerate}
\item \textsf{description}
\end{itemize}
\item Texto alineado horizontalmente
\begin{itemize}
\item \textsf{flushleft}
\item \textsf{center}
\item \textsf{flushright}
\end{itemize}
\item Texto mecanografiado
\begin{itemize}
\item \textsf{verbatim}
\end{itemize}
\end{itemize}
\end{itemize}
\end{itemize}
\end{micodigo}

\end{document}

The result:

enter image description here

As Ignasi suggested in his comment, you can avoid the use of literate by using the listingsutf8 package. In this case, you load tcolorbox as follows:

\usepackage[many]{tcolorbox}
\tcbuselibrary{listingsutf8}
\tcbset{listing utf8=latin1}

A complete example:

\documentclass[twoside]{tufte-handout}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[spanish,mexico]{babel}    
\usepackage[many]{tcolorbox}
\tcbuselibrary{listingsutf8}
\tcbset{listing utf8=latin1}

 \lstset{%
     language=[LaTeX]TeX,
     basicstyle=\ttfamily\small,
     commentstyle=\itshape\ttfamily\small,
     showspaces=false,
     showstringspaces=false,
     breaklines=true,
     breakautoindent=true,
     captionpos=t,
}

\tcbset{
  example/.style 2 args={
    colframe=red!50!black, 
    colback=red!50!black!5,
    fonttitle=\small\sffamily\bfseries, 
    fontupper=\small,   
    fontlower=\small,
    text outside listing,
    title={Ejemplo~\thetcbcounter: #1},label={#2}},
}
\newtcblisting[auto counter]{micodigo}[3][]{example={#2}{#3},#1}

\begin{document}

\begin{micodigo}{ejemplo de listas}{eje:lista}
\begin{itemize}
\item Introducción a \LaTeX
\item Texto con \LaTeX
\begin{itemize}
\item Reglas básicas
\item Entornos de texto
\begin{itemize}
\item Incisos, citaciones y poemas
\begin{itemize}
\item \textsf{quote}
\item \textsf{quotation}
\item \textsf{verse}
\end{itemize}
\item Listas
\begin{itemize}
\item \textsf{itemize}
\item \textsf{enumerate}
\item \textsf{description}
\end{itemize}
\item Texto alineado horizontalmente
\begin{itemize}
\item \textsf{flushleft}
\item \textsf{center}
\item \textsf{flushright}
\end{itemize}
\item Texto mecanografiado
\begin{itemize}
\item \textsf{verbatim}
\end{itemize}
\end{itemize}
\end{itemize}
\end{itemize}
\end{micodigo}

\end{document}
  • 1
    I think with \tcbuselibrary{listingsutf8} and without literate, there's no problem with accented characters. – Ignasi Jun 25 '15 at 18:09
  • @Ignasi You're right. I've updated the answer. Thanks. – Gonzalo Medina Jun 25 '15 at 18:21
  • Very nice solution, unfortunately that example is too long and doesn't fit in the page. – Aradnix Jun 26 '15 at 8:01
3

listings cannot handle utf8 characters. Use xelatex instead of pdflatex. However, only two byte characters are possible.

\documentclass[twoside]{tufte-handout}
\usepackage{fontspec}
\usepackage{showexpl}

\lstloadlanguages{[LaTeX]Tex}
\lstset{%
    basicstyle=\ttfamily\small,
    commentstyle=\itshape\ttfamily\small,
    showspaces=false,
    showstringspaces=false,
    breaklines=true,
    breakautoindent=true,
    captionpos=t,
}

\begin{document}
\begin{LTXexample}  
\begin{itemize}
  \item Introducción a \LaTeX
  \item Texto con \LaTeX
  \begin{itemize}
    \item Reglas básicas
    \item Entornos de texto
    \begin{itemize}
      \item Incisos, citaciones y poemas
      \begin{itemize}
        \item \textsf{quote}
        \item \textsf{quotation}
        \item \textsf{verse}
      \end{itemize}
      \item Listas
     \begin{itemize}
        \item \textsf{itemize}
        \item \textsf{enumerate}
        \item \textsf{description}
      \end{itemize}
      \item Texto alineado horizontalmente
      \begin{itemize}
        \item \textsf{flushleft}
        \item \textsf{center}
        \item \textsf{flushright}
      \end{itemize}
      \item Texto mecanografiado
      \begin{itemize}
        \item \textsf{verbatim}
      \end{itemize}
    \end{itemize}
  \end{itemize}
\end{itemize}
\end{LTXexample}
\end{document}

enter image description here

  • 1
    This works with normal listings, but not with showexpl. The non-ascii-chars get lost when the tempory file is written. – Ulrike Fischer Jun 25 '15 at 16:46
  • 1
    @GonzaloMedina: One can get the correct output by adding the literate-option to the explpreset key, but one still get errors. tcolorbox is certainly a better option than showexpl. – Ulrike Fischer Jun 25 '15 at 17:30

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.