从 tcolorbox 中的外部文件读取代码并获取 pdflatex> !段落在 \tcb@set@label 完成之前结束

从 tcolorbox 中的外部文件读取代码并获取 pdflatex> !段落在 \tcb@set@label 完成之前结束

我想在tcolorbox新定义的\texexptitled环境中从外部文件读取代码,但无法成功(请参阅 MWE)。没有这个,我的 MWE 还会抛出另外两个错误pdflatex> ! paragraph ended before \tcb@set@label was complete.。任何帮助都将不胜感激。提前致谢。


平均能量损失


\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%


\usepackage{filecontents}
\begin{filecontents*}{exampleTest.tex}
   Welcome to \LaTeX.
\end{filecontents*}


%Definition of New Environments
\tcbset{
texexp/.style={colframe=green!85!black, colback=red!5!white,
coltitle=red!50!yellow!3!black, left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
fonttitle=\small\textsc\bfseries, fontupper=\small, fontlower=\small},
example/.style 2 args={texexp,
title={Listing \thetcbcounter: #1},label={#2}},
}


\newtcblisting{texexp}[1]{texexp,#1}

\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
example={#2}{#3},#1}

\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
example={#2}{#3},#1}


\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}


\tcbset{listing engine={listings}}
\begin{document}

% Redefine the document environment within a group

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\tcbinputlisting{%
  listing file=exampleTest.tex
, colback=red!5!white
, colframe=red!25
, left=6mm
, listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}
 }

\endgroup

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{texexptitled}{Example}

\begin{document}
Here, we use Example  with a title line.
\end{document}

\end{texexptitled}

\endgroup



\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\texexptitledinputlisting{% Just tried. Not Working.
  listing file=exampleTest.tex
, colback=red!5!white
, colframe=red!25
, left=6mm
, listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}
 }

\endgroup


\end{document}

答案1

我不确定我是否完全理解了您的意图。但是,我纠正了 Christian Hupfer 已经提到的错误,并制作了一个新的宏,\texexptitledinputlisting希望它对您自己的扩展有帮助。我保留其余部分不变,只做了一些小的例外/更正。

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%


\usepackage{filecontents}
\begin{filecontents*}{exampleTest.tex}
   Welcome to \LaTeX.
\end{filecontents*}


%Definition of New Environments
\tcbset{
  texexp/.style={colframe=green!85!black, colback=red!5!white,
  coltitle=red!50!yellow!3!black, left=6mm,
  listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
  fonttitle=\small\scshape\bfseries, fontupper=\small, fontlower=\small},
  example/.style 2 args={texexp,title={Listing \thetcbcounter: #1},label={#2}},
}


\newtcblisting{texexp}[1]{texexp,#1}

\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
  example={#2}{#3},#1}

\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
  example={#2}{#3},#1}

\newtcbinputlisting[use counter from=texexptitled]{\texexptitledinputlisting}[4][]{%
  example={#3}{#4},listing file=#2,#1}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}


\tcbset{listing engine={listings}}
\begin{document}

\section{Test}

% Redefine the document environment within a group

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\tcbinputlisting{%
  listing file=exampleTest.tex, colback=red!5!white,
  colframe=red!25, left=6mm,
  listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}
}

\endgroup

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{texexptitled}{Example}{examplelabel}
\begin{document}
Here, we use Example  with a title line.
\end{document}
\end{texexptitled}

\endgroup


\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\texexptitledinputlisting[
  colback=red!5!white,
  colframe=red!25,
  left=6mm,
  listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}]%
  {exampleTest.tex}{Example title}{examplelabel2}

\endgroup

\end{document}

在此处输入图片描述

相关内容