2

I'm working currently on a beamer-document and want to use LaTeX-code. Therefore I set up the listings-package in the following way:

\documentclass[12pt]{beamer}
\usepackage{listings}
\usepackage{xcolor}
\definecolor{keywordcolor}{HTML}{102D5D}
\definecolor{darkgreen}{rgb}{0,.6,0}
\definecolor{commentcolor}{HTML}{562B71}
\lstset{
    language=[LaTeX]TeX,
    texcsstyle=*\bf\color{keywordcolor},
    numbers=left,
    breaklines=true,
    keywordstyle=\color{darkgreen},
    commentstyle=\itshape\color{commentcolor},
    morekeywords={},
    otherkeywords={$, \{, \}, \[, \]},
    tabsize=2,
    frame=leftline,
    basicstyle=\footnotesize\ttfamily,
    backgroundcolor=\color{black!5}}

\begin{document}

\begin{frame}[fragile]

    \begin{lstlisting}
    \usepackage[Option]{Paket}
    \end{lstlisting}

\end{frame}

\end{document}

The tex-file otherkeywords contains {$, \{, \}, \[, \]}. If I use \input, I'll get a bunch of errors. If I use the content for itself, I'll get the right result. Am I doing something wrong?

The errors:

Improper alphabetic constant backgroundcolor=\color{black!5}}
Extra \else backgroundcolor=\color{black!5}}
Improper alphabetic constant backgroundcolor=\color{black!5}}
Improper alphabetic constant backgroundcolor=\color{black!5}}
Paragraph ended before \filehook@@ensureext was complete
Missing \endcsname inserted

I tried all the suggestions at another question, but no success for me.

Sincerely

  • Tell as what type of errors those are. Also an MWE would be nice, that reproduces those errors. – polemon Jun 1 '14 at 21:36
  • You can't use \input there. – egreg Jun 1 '14 at 21:48
  • otherkeywords wants a list of (possibly escaped) characters, not the instructions to produce a list of characters. – egreg Jun 1 '14 at 22:08
3

If the file otherkeywords.tex contains the value of key otherkeywords, then the value can given as macro in this case. Package catchfile helps in storing the contents of a file in a macro:

\usepackage{catchfile}
\CatchFileDef{\otherkeywords}{otherkeywords}{\endlinechar=-1\relax}
\lstset{
    % ...,
    otherkeywords=\otherkeywords,
    % ...,
}

\input can not be used here, because it is not expandable and it should produce the list with one expansion step. But \input can be used with \lstset:

% otherkeywords-alternative.tex
\lstset{otherkeywords={$, \{, \}, \[, \]}}

and in the main file:

\lstset{%
  % old entries without otherkeywords
}
\input{otherkeywords-alternative}
2

A variation of Heiko's answer, with a new key:

\documentclass[12pt]{beamer}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{catchfile}
\definecolor{keywordcolor}{HTML}{102D5D}
\definecolor{darkgreen}{rgb}{0,.6,0}
\definecolor{commentcolor}{HTML}{562B71}

\makeatletter
\lst@Key{otherkeywordsfile}{}{%
  \let\lst@otherkeywords\@empty
  \let\lst@temp\@empty
  \if\relax\detokenize{#1}\relax\else
    \CatchFileDef{\lst@temp}{#1}{\endlinechar=\m@ne}%
  \fi
  \expandafter\lst@for\expandafter{\lst@temp}\do{%
    \lst@MakeActive{##1}%
    \lst@lExtend\lst@otherkeywords{%
      \expandafter\lst@CArg\lst@temp\relax\lst@CDef
        {}\lst@PrintOtherKeyword\@empty}%
  }%
}
\makeatother

\lstset{
    language=[LaTeX]TeX,
    texcsstyle=*\bfseries\color{keywordcolor},
    numbers=left,
    breaklines=true,
    keywordstyle=\color{darkgreen},
    commentstyle=\itshape\color{commentcolor},
    morekeywords={},
%    otherkeywords={$, \{, \}, \[, \]},
    otherkeywordsfile=otherkeywords, % <---- the value is the file name
    tabsize=2,
    frame=leftline,
    basicstyle=\footnotesize\ttfamily,
    backgroundcolor=\color{black!5}}

\begin{document}

\begin{frame}[fragile]

    \begin{lstlisting}
    \usepackage[Option]{Paket}
    \end{lstlisting}

\end{frame}

\end{document}

In the otherkeywords.tex file the braces can be omitted, so it can be

$, \{, \}, \[, \]

A similar idea could be used for morekeywords.

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.