自动检测何时出现在 Mdframed 框架标题中

自动检测何时出现在 Mdframed 框架标题中

我希望能够检测我何时处于frametitle={}环境中mdframed。一种方法是在 内手动设置一个标志\frametitle={},但我想知道是否有其他方法可以检测这种情况,这样我就不必调整 的所有使用frametitle={}

下面的 MWE 产生了所需的结果:

在此处输入图片描述

因为它设定了一个toggle

frametitle={\toggletrue{InMdframedFrameTitle}\Title}

但我希望达到同样的结果,而不仅仅是

frametitle={\Title}

相关问题:

梅威瑟:

\documentclass{article}
\usepackage{mdframed}
\usepackage{etoolbox}
\usepackage{xcolor}

\mdfdefinestyle{MyStyle}{%
    frametitlebackgroundcolor=red!40,
    frametitlerule=true, 
}

\newtoggle{InMdframedFrameTitle}
\newcommand*{\TitleColor}{.}
\newcommand*{\Title}{%
    \iftoggle{InMdframedFrameTitle}{%
        \renewcommand{\TitleColor}{.}%
    }{%
        \renewcommand{\TitleColor}{red}%
    }%
    \textcolor{\TitleColor}{Newton's Laws}%
}

\begin{document}

\begin{mdframed}[
    style=MyStyle, 
    frametitle={\toggletrue{InMdframedFrameTitle}\Title}% <--- Want to remove the \toggletrue
    %frametitle={\Title}%                                 <--- and use just this.
    ]
    One of the things that Newton is known for is \Title, which refer to \dots.
\end{mdframed}
\end{document}

答案1

修补\mdfframedtitleenv以在开始时设置切换似乎有效:

\makeatletter
    \let\OLD@mdfframedtitleenv\mdfframedtitleenv
    \let\mdfframedtitleenv\relax
    \newrobustcmd*\mdfframedtitleenv[1]{%
        \global\toggletrue{InMdframedFrameTitle}%  <--- Set toggle
        \OLD@mdfframedtitleenv{#1}%                <--- Execute original definition
        \global\togglefalse{InMdframedFrameTitle}% <--- Clear toggle
    }
\makeatother

代码:

\documentclass{article}
\usepackage{mdframed}
\usepackage{etoolbox}
\usepackage{xcolor}

\makeatletter
    \let\OLD@mdfframedtitleenv\mdfframedtitleenv
    \let\mdfframedtitleenv\relax
    \newrobustcmd*\mdfframedtitleenv[1]{%
        \global\toggletrue{InMdframedFrameTitle}%  <--- Set toggle
        \OLD@mdfframedtitleenv{#1}%                <--- Execute original definition
        \global\togglefalse{InMdframedFrameTitle}% <--- Clear toggle
    }
\makeatother

\mdfdefinestyle{MyStyle}{%
    frametitlebackgroundcolor=red!40,
    frametitlerule=true, 
}

\newtoggle{InMdframedFrameTitle}
\newcommand*{\TitleColor}{.}
\newcommand*{\Title}{%
    \iftoggle{InMdframedFrameTitle}{%
        \renewcommand{\TitleColor}{.}%
    }{%
        \renewcommand{\TitleColor}{red}%
    }%
    \textcolor{\TitleColor}{Newton's Laws}%
}

\begin{document}

\begin{mdframed}[
    style=MyStyle, 
    %frametitle={\toggletrue{InMdframedFrameTitle}\Title}% <--- Want to remove the \toggletrue
    frametitle={\Title}%                                 <--- and use just this.
    ]
    One of the things that Newton is known for is \Title, which refer to \dots.
\end{mdframed}
\end{document}

相关内容