Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to write a marginnote command that detects whether it is called within a mdframed environment, in particular I want to be able to distinguish the following

  1. normal text

  2. normal math

  3. text in mdframed

  4. math in mdframed

I'm not a TeX expert and I've tried looking at \ifinner, \ifhmode, \ifvmode, \ifmmode, but I cannot distinguish the mdframed instances. See the code below to demonstrate my task. Is it possible to do what I want?

\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}
share|improve this question

2 Answers 2

up vote 9 down vote accepted

You can add to mdframed the setting of a conditional:

\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}

Thus you can examine \ifinmdframed to see if you are in a mdframed environment.

share|improve this answer
    
Can you explain why you're setting the boolean globally? –  cgnieder Jan 16 '13 at 23:14
    
@cgnieder Good question. :) Just laziness in looking at the documentation of etoolbox. –  egreg Jan 16 '13 at 23:16
    
Ah, I see :) And I was sure there was some not so obvious but nonetheless profound reason... –  cgnieder Jan 16 '13 at 23:24
\def\md@envname{mdframed}
...
\ifx\@currenvir\md@envname in mdframed \fi

will detect if the current environment is mdframed (it is only true at the top level of the frame, it is false again inside the align* in the mdframed as there \@currenvir will be align*

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.