在最近问题我找到了如何禁用mdframed
显示任何内容的所有实例的方法。这可以通过 优雅地完成\RenewDocumentEnvironment{mdframed}{+b}{}{}
。
现在,我想知道如何将其仅应用于特定mdframed
样式。这是一个可行的最小示例:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{show}{leftmargin=1cm,linecolor=gray, roundcorner=10pt, align=center, userdefinedwidth=.9\linewidth, nobreak=true}
\mdfdefinestyle{draftA}{hidealllines=true,backgroundcolor=blue!20}
\mdfdefinestyle{draftB}{hidealllines=true,backgroundcolor=green!20}
%\RenewDocumentEnvironment{mdframed}{+b}{}{} % With this line uncommented, all mdframed are not sent to the output
\begin{document}
Text before the environment
\begin{mdframed}[style=draftA]
I do not want this content to be printed.
\end{mdframed}
\begin{mdframed}[style=draftB]
I do not want this content to be printed.
\end{mdframed}
\begin{mdframed}[style=show]
I do want this content to be printed.
\end{mdframed}
Text after the environment
\end{document}
答案1
有大卫·卡莱尔的大力帮助对于具体问题,我可以提供解决方案:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{mdframed}
\mdfdefinestyle{show}{leftmargin=1cm,linecolor=gray, roundcorner=10pt, align=center, userdefinedwidth=.9\linewidth, nobreak=true}
\mdfdefinestyle{draftA}{hidealllines=true,backgroundcolor=blue!20}
\mdfdefinestyle{draftB}{hidealllines=true,backgroundcolor=green!20}
\NewCommandCopy\oldmdframed\mdframed
\NewCommandCopy\endoldmdframed\endmdframed
\RenewDocumentEnvironment{mdframed}{o +b}{% begin part
\ifstrequal{#1}{style=show}{% true part
\oldmdframed[style=show]#2\endoldmdframed}{% false part
}}{% end part
}
\begin{document}
Text before the environment
\begin{mdframed}[style=draftA]
I do not want this content to be printed.
\end{mdframed}
\begin{mdframed}[style=draftB]
I do not want this content to be printed.
\end{mdframed}
\begin{mdframed}[style=show]
I do want this content to be printed.
\end{mdframed}
Text after the environment
\end{document}
您将获得:
请注意,该命令\ifstrequal
来自etoolbox
包,由 自动加载mdframed
。