我打算创建一个代码环境来加速和清理我的 TeX 代码。这个想法是,它可以在前面加上 5mm 的空格,section
也subsection
可以subsubsection
选择使用 在同一页中一起打印minipage
。
到目前为止我已经得到了以下代码:
\documentclass{article}
\usepackage{xparse}
\usepackage[fontsize=12pt]{fontsize}
% Code
\usepackage{shellesc, minted}
\setminted
{
linenos, % line number
autogobble, % line trim
tabsize= 4, % tab size
obeytabs, % tab alignment
breaklines, % break lines
}
\usemintedstyle{fruity}
\usepackage{xcolor}
\colorlet{Background}{white!5!black}
\pagecolor{black}
\color{white}
\usepackage{tcolorbox}
\tcbuselibrary
{
breakable, % allow page break
minted, xparse, listings, % code minted
}
\tcbset%
{ every box/.style= {
coltext= white, % text color
coltitle= white, % title color
fonttitle= \bfseries, % title font
opacityframe= 0, % frame opacity
colback= Background, % background color
colframe= Background, % border color
width= \linewidth, % Width
}}
% code
\DeclareTCBListing{codeBox}
{ s t1 t2 t3 O{5mm} O{left=2.5em} m m }
{
before=
{
\vspace{#5}
\IfBooleanF{#1}{\noindent\begin{minipage}{\linewidth}\relax}
\IfBooleanT{#2}{ \section{#8}\relax}
\IfBooleanT{#3}{ \subsection{#8}\relax}
\IfBooleanT{#4}{\subsubsection{#8}\relax}
},
\IfBooleanTF{#1}
{breakable,}%
{after= {\end{minipage}\relax},}%
listing only,
listing engine= minted,
minted language= #7,
minted options={},
#6
}
\begin{document}
%% Code breaks when using environment, but not when declaring
%% Outputs Error 1
%\begin{codeBox}{python}{env test}
%def hello(foo):
% print(f"Hello world ${foo}", 1+1, True)
%\end{codeBox}
%% Code breaks if i try to insert a section before tcblisting
%% Outputs Error 2
% \section{before}
\begin{tcblisting}
{
before=%
{%
\vspace{5mm}
\noindent\begin{minipage}{\linewidth}
\section{manual test}
},
after= {\end{minipage}},
listing only,
listing engine= minted,
minted language= python,
minted options={},
left=2.5em,
}
def hello(foo):
print(f"Hello world ${foo}", 1+1, True)
\end{tcblisting}
\section{after}
\end{document}
由于它是用 minted 实现的,因此要进行编译,请在文件位置的终端上使用以下命令:lualatex --shell-escape ./*.tex
产生的错误如下:
错误 1:
! Incomplete \iffalse; all text was ignored after line 69.
<inserted text>
\fi
<*> ./Untitled.tex
第 69 行:\begin{codeBox}{python}{env test}
错误 2:
! Extra }, or forgotten \endgroup.
\endminipage ...pagefalse \color@endgroup \egroup
\expandafter \@iiiparbox \...
l.93 \end{tcblisting}
为什么不能在环境
\section{}
之前添加?tcblisting
声明的环境出了什么问题
codeBox
?