![定义“tcolorbox”环境时避免重复代码](https://linux22.com/image/436629/%E5%AE%9A%E4%B9%89%E2%80%9Ctcolorbox%E2%80%9D%E7%8E%AF%E5%A2%83%E6%97%B6%E9%81%BF%E5%85%8D%E9%87%8D%E5%A4%8D%E4%BB%A3%E7%A0%81.png)
有没有办法分解以下代码,其中环境doclatex
和之间的唯一区别doclatex-flat
在于使用listing side text
?
\documentclass[12pt]{article}
\RequirePackage{tcolorbox}
\tcbuselibrary{minted, breakable}
\newtcblisting{doclatex}{%
breakable,
sharp corners,
left = 1mm, right = 1mm,
bottom = 1mm, top = 1mm,
listing side text
}
\newtcblisting{doclatex-flat}{
breakable,
sharp corners,
left = 1mm, right = 1mm,
bottom = 1mm, top = 1mm,
}
\begin{document}
\begin{doclatex}
$just + A = test$
\end{doclatex}
\begin{doclatex-flat}
$just + A = test$
\end{doclatex-flat}
\end{document}
答案1
tcolorbox
的 key-val 方案来自pgfkeys
,它由 或 自动加载pgf
。因此,您可以使用键处理程序tikz
定义样式键。/.style
\documentclass[12pt]{article}
\RequirePackage{tcolorbox}
\tcbuselibrary{minted, breakable}
\tcbset{
doc latex common/.style={
breakable,
sharp corners,
left = 1mm, right = 1mm,
bottom = 1mm, top = 1mm,
}
}
\newtcblisting{doclatex}{%
doc latex common,
listing side text
}
\newtcblisting{doclatex-flat}{
doc latex common,
bottom = 1mm, top = 1mm,
}
\begin{document}
\begin{doclatex}
$just + A = test$
\end{doclatex}
\begin{doclatex-flat}
$just + A = test$
\end{doclatex-flat}
\end{document}