用 minted 宏包制作 example 环境

基本概念

example 环境指一边为源代码区,一边为编译后的效果区,我见过的 example 环境的制作方法有

原因

比较喜欢 lshort-zh-cnexample 环境的风格

实现

找到其源码,MWE 如下

\documentclass{article}
\usepackage{verbatim,xcolor}

\makeatletter
\newwrite\example@out
\newlength\savefboxrule
\newlength\savefboxsep
\edef\example@name{\jobname-example.aux}
\newenvironment{example}%
{\begingroup\@bsphack
\immediate\openout\example@out=\example@name
\let\do\@makeother\dospecials\catcode`\^^M\active
\def\verbatim@processline{\immediate\write\example@out{\the\verbatim@line}}%
\verbatim@start}%
{\immediate\closeout\example@out\@esphack\endgroup%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{0.45\textwidth}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\verbatiminput{\example@name}%
\end{minipage}%
}%
\hfill%
\fbox{%
\begin{minipage}[c]{0.45\textwidth}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\setlength{\parskip}{1ex plus 0.4ex minus 0.2ex}%
\normalsize\input{\example@name}%
\end{minipage}%
}%
\endtrivlist
}
\makeatother
\begin{document}
\begin{example}
\begin{equation}\label{eq:1}
E = mc^2
\end{equation}
The equation~(\ref{eq:1}) is a
famous mass energy equation.
\end{example}
\end{document}

效果为

不满意其代码区效果,想用 minted 宏包配合 sourcecodepro 宏包制作代码区,做了一部分尝试,更多讨论见《如何让 lshort-zh-cn 的 example 环境中的代码部分变为用 minted 宏包排版?

结果

水平结构

\documentclass{ctexart}
\usepackage{minted}
\setminted{formatcom=\xeCJKVerbAddon}
\usepackage{sourcecodepro}
\usepackage{xcolor}
\makeatletter
\newlength\savefboxrule
\newlength\savefboxsep
\edef\example@name{\jobname-example.aux}
\newenvironment{hexample}%
{%
\renewcommand{\minted@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\let\FV@ProcessLine\minted@write@detok
\immediate\openout\FV@OutFile ##1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\minted@FVB@VerbatimOut{\example@name}}%
{\minted@FVE@VerbatimOut%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{0.45\textwidth}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}%
\end{minipage}%
}%
\hfill%
\fbox{%
\begin{minipage}[c]{0.45\textwidth}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\setlength{\parskip}{1ex plus 0.4ex minus 0.2ex}%
\normalsize\input{\example@name}%
\end{minipage}%
}%
\endtrivlist
}
\makeatother

\begin{document}
\begin{hexample}
\begin{equation}\label{eq:1}
E = mc^2
\end{equation}
公式~(\ref{eq:1}) 是
著名的质能方程。
\end{hexample}
\end{document}

效果为

竖直结构

\documentclass{ctexart}
\usepackage{minted}
\setminted{formatcom=\xeCJKVerbAddon}
\usepackage{sourcecodepro}
\usepackage{xcolor}
\makeatletter
\newlength\savefboxrule
\newlength\savefboxsep
\edef\example@name{\jobname-example.aux}
\newenvironment{vexample}%
{%
\renewcommand{\minted@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\let\FV@ProcessLine\minted@write@detok
\immediate\openout\FV@OutFile ##1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\minted@FVB@VerbatimOut{\example@name}}%
{\minted@FVE@VerbatimOut%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\begin{minipage}[c]{\textwidth}
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{\textwidth-0.03\textwidth-0.8pt}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}%
\end{minipage}%
}\\
\fbox{%
\begin{minipage}[c]{\textwidth-0.03\textwidth-0.8pt}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\setlength{\parskip}{1ex plus 0.4ex minus 0.2ex}%
\normalsize\input{\example@name}%
\end{minipage}%
}%
\end{minipage}
\endtrivlist
}
\makeatother
\begin{document}
\begin{vexample}
\begin{equation}\label{eq:1}
E = mc^2
\end{equation}
公式~(\ref{eq:1}) 是著名的质能方程。
\end{vexample}
\end{document}

效果为

浮动体专用

\documentclass{ctexart}
\usepackage{minted}
\setminted{formatcom=\xeCJKVerbAddon}
\usepackage{sourcecodepro}
\usepackage{xcolor}
\usepackage{mwe}
\makeatletter
\newlength\savefboxrule
\newlength\savefboxsep
\edef\example@name{\jobname-example.aux}
\newenvironment{floatexample}%
{%
\renewcommand{\minted@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\let\FV@ProcessLine\minted@write@detok
\immediate\openout\FV@OutFile ##1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\minted@FVB@VerbatimOut{\example@name}}%
{\minted@FVE@VerbatimOut%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{\textwidth-0.03\textwidth-0.8pt}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}%
\end{minipage}%
}\par
\input{\example@name}%
\endtrivlist
}
\makeatother
\begin{document}
\begin{floatexample}
\begin{figure}[htbp]
\centering
\includegraphics{example-image.pdf}
\caption{content}
\end{figure}
\end{floatexample}
\end{document}

效果为

合并

\documentclass{ctexart}
\usepackage{minted}
\setminted{formatcom=\xeCJKVerbAddon}
\usepackage{sourcecodepro}
\usepackage{xcolor}
\usepackage{mwe}
\makeatletter
\newlength\savefboxrule
\newlength\savefboxsep
\edef\example@name{\jobname-example.aux}
\newenvironment{hexample}%
{%
\renewcommand{\minted@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\let\FV@ProcessLine\minted@write@detok
\immediate\openout\FV@OutFile ##1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\minted@FVB@VerbatimOut{\example@name}}%
{\minted@FVE@VerbatimOut%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{0.45\textwidth}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}%
\end{minipage}%
}%
\hfill%
\fbox{%
\begin{minipage}[c]{0.45\textwidth}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\setlength{\parskip}{1ex plus 0.4ex minus 0.2ex}%
\normalsize\input{\example@name}%
\end{minipage}%
}%
\endtrivlist
}
\newenvironment{vexample}%
{%
\renewcommand{\minted@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\let\FV@ProcessLine\minted@write@detok
\immediate\openout\FV@OutFile ##1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\minted@FVB@VerbatimOut{\example@name}}%
{\minted@FVE@VerbatimOut%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\begin{minipage}[c]{\textwidth}
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{\textwidth-0.03\textwidth-0.8pt}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}%
\end{minipage}%
}\\
\fbox{%
\begin{minipage}[c]{\textwidth-0.03\textwidth-0.8pt}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\setlength{\parskip}{1ex plus 0.4ex minus 0.2ex}%
\normalsize\input{\example@name}%
\end{minipage}%
}%
\end{minipage}
\endtrivlist
}
\newenvironment{floatexample}%
{%
\renewcommand{\minted@FVB@VerbatimOut}[1]{%
\@bsphack
\begingroup
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\FV@DefineTabOut
\let\FV@ProcessLine\minted@write@detok
\immediate\openout\FV@OutFile ##1\relax
\let\FV@FontScanPrep\relax
\let\@noligs\relax
\FV@Scan}
\minted@FVB@VerbatimOut{\example@name}}%
{\minted@FVE@VerbatimOut%
\trivlist\item\relax
\setlength{\savefboxrule}{\fboxrule}%
\setlength{\savefboxsep}{\fboxsep}%
\setlength{\fboxsep}{0.015\textwidth}%
\setlength{\fboxrule}{0.4pt}%
\fcolorbox[gray]{0}{0.95}{%
\begin{minipage}[c]{\textwidth-0.03\textwidth-0.8pt}%
\setlength{\fboxrule}{\savefboxrule}%
\setlength{\fboxsep}{\savefboxsep}%
\small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}%
\end{minipage}%
}\par
\input{\example@name}%
\endtrivlist
}
\makeatother
\begin{document}
\begin{hexample}
\begin{equation}\label{eq:1}
E = mc^2
\end{equation}
公式~(\ref{eq:1}) 是著名的质能方程。
\end{hexample}
\begin{vexample}
\begin{equation}\label{eq:2}
E = mc^2
\end{equation}
公式~(\ref{eq:2}) 是著名的质能方程。
\end{vexample}
\begin{floatexample}
\begin{figure}[htbp]
\centering
\includegraphics[width=5cm]{example-image.pdf}
\caption{content}
\end{figure}
\end{floatexample}
\end{document}

效果为

注意事项

  • 编译环境:TeX\TeX Live 2020
  • 编译方式为 xelatex --shell-escape
  • 电脑需安装了 minted 宏包所依赖的库「Pygments」
  • 若需修改代码区样式,修改 \small\inputminted[breakanywhere,breaklines=true,numbers=left]{latex}{\example@name}% 中的 \inputminted 选项即可

相关链接

文章作者: sikouhjw
文章链接: https://sikouhjw.github.io/2020/04/29/2020-04-29-Make-an-example-environment-with-minted/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 sikouhjw.github.io
支付宝捐赠
微信捐赠