禁用 mdframed 包显示任何内容的特定样式

禁用 mdframed 包显示任何内容的特定样式

在最近问题我找到了如何禁用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

相关内容