\input 产生错误,复制粘贴不会

\input 产生错误,复制粘贴不会

我目前正在处理一份 beamer 文档,并希望使用 LaTeX 代码。因此,我按以下方式设置 listings 包:

\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}

tex 文件otherkeywords包含{$, \{, \}, \[, \]}。如果我使用\input,我会得到一堆错误。如果我将内容用作其本身,我会得到正确的结果。我做错了什么吗?

错误:

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

我尝试了所有建议另一个问题,但我没有成功。

真挚地

答案1

如果文件otherkeywords.tex包含 key 的值otherkeywords,则在这种情况下可以将该值作为宏给出。包catchfile有助于将文件的内容存储在宏中:

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

\input不能在这里使用,因为它不可扩展,它应该生成带有扩展步骤。但\input可以与以下方法一起使用\lstset

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

在主文件中:

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

答案2

Heiko 答案的变体,但添加了一个新关键词:

\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}

otherkeywords.tex文件中,括号可以省略,因此可以

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

类似的想法也可用于morekeywords

相关内容