我对 LaTeX 还很陌生,最近才开始学习 tikz。我使用“showexpl”和“listings”包在 overleaf 中生成 tikz 代码及其输出的示例。几乎所有文件序言都太大了。因此,我创建了几个包(.sty 文件),每个包都包含我的序言的小部分,然后将它们全部放在单个 usepackage 中的主文件中。但是,当使用 showexpl 执行此操作时,我收到错误“未定义的控制序列”。虽然文件(main.tex)编译正常,没有任何问题,但仍然会产生错误。我该如何摆脱错误?这是我的源代码的链接:
https://www.overleaf.com/read/nwkdqyrmdcvf
https://www.overleaf.com/8629643923mmtjtdbfmrfs
EDIT-1:正如评论中所述,我已尽力在此处发布 MWE,尽管它很大而且很混乱,因为我必须从多个文件中收集几段代码并将它们放在一起,所以它变得很分散
\documentclass[11pt]{book}
\usepackage[dvipsnames,svgnames,x11names]{xcolor}
\usepackage[all]{tcolorbox}
\usepackage{showexpl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%==================================={geometry}
\usepackage{geometry}
\geometry{total={18cm,25cm},left=2cm}
%==================================={other}
\usepackage{tikz}
\usepackage{listings,keyval}
\lstloadlanguages{[AspectJ]java,[AlLaTeX]TeX,[ANSI]C,}
\lstloadaspects{keywordcomments,procnames,hyper,formats}
%==================================={lstnewenvironment}
\lstnewenvironment{code}[1][]
{\lstset{style=mystyle,language={[PTX2]ProTeX}}}{}
\lstMakeShortInline[style=mystyle,language={[PTX]ProTeX}]!
%==================================={lstdefinelanguage}
\lstdefinelanguage[PTX]{ProTeX}
{language=[AlLaTeX]TeX,
basicstyle=\ttfamily\color{black},
moredelim=**[l][\color{blue}]{\\},
moredelim=**[s][\color{SpringGreen3}]{[}{]},
moredelim=**[s][\color{SpringGreen3}]{[<}{>]},
moredelim=**[s][\color{red}]{\{}{\}},
moredelim=**[s][\color{red}]{\{<}{>\}},
moredelim=**[s][\color{Turquoise2}]{<}{>}}
%==================================={lstdefinelanguage-2}
\lstdefinelanguage[PTX2]{ProTeX}
{language=[AlLaTeX]TeX,
basicstyle=\ttfamily\color{black},
morekeywords={lstloadlanguages,lstloadaspects,lstset,lstalias,lstdefinelanguage,lstdefinestyle,lstdefineformat,lstnewenvironment,
lstMakeShortInline},
keywordstyle=\color{red}\bfseries,
moretexcs={color,lstlistoflistings,thelstlisting},
texcsstyle=*\color{blue}\bfseries,
commentstyle=\color{SpringGreen4}\itshape,
}
%==================================={lstdefinestyle}
\lstdefinestyle{mystyle}
%...................................(Line numbers)
{numbers=left,
numberfirstline=true,
numberstyle=\bfseries\scriptsize\color{black},
numbersep=10pt,
firstnumber=auto,
%...................................(frame-shape)
frame=single,
frame=TBRL,
framesep=0pt,
rulesep=2pt,
framerule=0pt,
xleftmargin=1cm,
framexleftmargin=1cm,
framextopmargin=10pt,
framexbottommargin=10pt,
%...................................(frame-color)
backgroundcolor=\color{black!2},
rulecolor=\color{white},
fillcolor=\color{black!2},
rulesepcolor=\color{blue},
escapeinside={?}{?},
%...................................(others)
breaklines=true,
breakindent=0pt}
%==================================={lstdefinestyle-2}
\lstdefinestyle{explstyle}
%...................................(Line numbers)
{numbers=left,
numberfirstline=true,
numberstyle=\bfseries\scriptsize\color{black},
numbersep=10pt,
firstnumber=auto,
%...................................(frame-shape)
frame=single,
frame=TBRL,
framesep=0pt,
rulesep=2pt,
framerule=0pt,
xleftmargin=1cm,
framexleftmargin=1cm,
framextopmargin=10pt,
framexbottommargin=10pt,
%...................................(frame-color)
backgroundcolor=\color{SpringGreen4!5},
rulecolor=\color{SpringGreen4!5},
fillcolor=\color{SpringGreen4!5},
rulesepcolor=\color{SpringGreen4!5},
escapeinside={?}{?},
%...................................(others)
breaklines=true,
breakindent=0pt}
\newtcolorbox{box2}
{colback=SpringGreen4!5,colframe=SpringGreen4,fonttitle=\bfseries}
\newtcbox{\minibox}[2][]
{colback=red!5,colframe=red,fonttitle=\bfseries,title={#2}#1}
\lstset{explpreset={style=mystyle,language={[PTX2]ProTeX}}}
\renewcommand\ResultBox{\fcolorbox{green}{Snow1}}
\setlength\ResultBoxSep{10pt}
\setlength\ResultBoxRule{2pt}
\begin{document}
\lstset{explpreset={style=explstyle,language={[PTX2]ProTeX}}}
\begin{box2}
\begin{LTXexample}[pos=r,justification=\centering]
\begin{document}
!Hello World!
\end{document}
\end{LTXexample}
\end{box2}
\end{document}
注意:在代码中,我曾\lstMakeShortInline[style=mystyle,language={[PTX]ProTeX}]!
使用 ! 符号作为内联代码片段的分隔符。注意:在 overleaf(或 TeXlive)中编译时,它会产生未定义控制序列的错误。注意:如果从到\begin{box2}
(\end{box2}
位于末尾)的行被任何随机文本替换,错误就会消失。所以,问题可能出在那几行,但我仍然无法弄清楚