编写命令来检测何时处于 mdframed 中

编写命令来检测何时处于 mdframed 中

我想编写一个marginnote命令来检测它是否在mdframed环境,特别是我希望能够区分以下内容

  1. 普通文本

  2. 普通数学

  3. 文本输入mdframed

  4. 数学mdframed

我不是 TeX 专家,我尝试查看\ifinner、、、,但我无法区分实例。请参阅下面的代码来演示我的任务。我可以做我想做的事吗\ifhmode\ifvmode\ifmmodemdframed

\documentclass[12pt, a4paper]{report}
\usepackage{amsmath, amssymb}
\usepackage[framemethod=tikz, roundcorner=8pt, shadow,% 
shadowsize=8pt, innerbottommargin=10pt, splittopskip=20pt, splitbottomskip=20pt]{mdframed}

\newcommand{\nmarginnote}[1]{
\ifhmode
Detected hmode
\fi
\ifvmode
Detected vmode
\fi
\ifmmode
Detected mmode 
\fi
\ifinner
Detected inner
\fi
}

\begin{document}

\section*{Normal text}
Test \nmarginnote{Test}

\section*{Normal math}
\begin{align*}
A + B = C \nmarginnote{Test}
\end{align*}

\section*{Normal text in mdframed}
\begin{mdframed}[backgroundcolor=blue!10, nobreak=true, frametitle={Frame}]
this is an example \nmarginnote{Test}
\end{mdframed}

\section*{Math in mdframed}
\begin{mdframed}[backgroundcolor=blue!10, nobreak=true, frametitle={Frame}]
\begin{align*}
A + B = C \nmarginnote{Test}
\end{align*}
\end{mdframed}

\end{document}

答案1

您可以添加mdframed条件设置:

\documentclass[12pt,a4paper]{report}
\usepackage{amsmath,amssymb}
\usepackage[framemethod=tikz,
  roundcorner=8pt,
  shadow, 
  shadowsize=8pt,
  innerbottommargin=10pt,
  splittopskip=20pt,
  splitbottomskip=20pt]{mdframed}

%%% Just for the test
\newcommand{\nmarginnote}[1]{%
  #1 (%
    \ifinmdframed MD-YES\else MD-NO\fi,
    \ifmmode MATH-YES\else MATH-NO\fi
  )%
}
%%% mdframed loads etoolbox so the commands are available
\newif\ifinmdframed
\AtBeginEnvironment{mdframed}{\inmdframedtrue}



\begin{document}

\section*{Normal text}
Test \nmarginnote{Test}

\section*{Normal math}
\begin{align*}
A + B = C \nmarginnote{Test}
\end{align*}

\section*{Normal text in mdframed}
\begin{mdframed}[backgroundcolor=blue!10, nobreak=true, frametitle={Frame}]
this is an example \nmarginnote{Test}
\end{mdframed}

\section*{Math in mdframed}
\begin{mdframed}[backgroundcolor=blue!10, nobreak=true, frametitle={Frame}]
\begin{align*}
A + B = C \nmarginnote{Test}
\end{align*}
\end{mdframed}

\end{document}

因此,您可以检查\ifinmdframed自己是否处于某个mdframed环境中。

答案2

\def\md@envname{mdframed}
...
\ifx\@currenvir\md@envname in mdframed \fi

将检测当前环境是否mdframed(它仅在框架的顶层为真,在align*框架内部再次为假mdframed,因为\@currenviralign*

相关内容