2

For theorems and alike I use mdframed package. So far I use solutions collected here on SE:

\documentclass[twoside]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{showframe}
    \usepackage{lipsum}

\usepackage[framemethod=tikz]{mdframed}
\mdfdefinestyle{box}{% common style
    topline=false, bottomline=false,
    rightline=false,
    linewidth=2pt,
    backgroundcolor=none,
apptotikzsetting={\tikzset{mdfbackground/.append style={%
                  fill=teal!40, fill opacity=0.25}}},
    frametitlefont=\sffamily\bfseries\color{black},
    splittopskip=1mm,
    frametitlebelowskip=0mm,
}
% Example
\mdtheorem[style=box,linecolor=teal!70
           ]{example}{Example}[chapter]%section

\begin{document}
\chapter{Test}
    \lipsum[2]    
\begin{example}[cross correlation]
\lipsum[2]
\end{example}

\newpage
    \lipsum[1-2]    
\begin{example}[cross correlation]
\lipsum[2]
\end{example}

\end{document}

which gives:

enter image description here

Now I like to define mdframed (nonbreakable) box which will be aware, if it is on odd or on even page, for example, that left line would be always in outer side of a page. So far I try the following:

  • define \mdfdefinestyle{box-L}{... rightline=false, ...} and \mdfdefinestyle{box-R}{... leftline=false ...}

  • select one of those boxes according if the example is on odd or even page

The letter I try to accomplish with the following code:

\mdtheorem[%
    \label{example:\thechapter.\theexample},
    \ifodd\pageref{example:\thechapter.\theexample}
  style=box-L
    \else
  style=box-R
    \fi,      
  linecolor=teal!70
            ]{example}{Example}[chapter]%section

Unfortunately, this my (naive) approach doesn't work. I receive error:

Use of \\mdframed doesn't mathch its definition.

Is there a way to accomplish may wish?

2

Perhaps the settings=... key might help here.

rightline and leftline are keys that trigger the mdf@rightline and mdf@leftline bool variables. Using

settings={\ifodd\value{page}\booltrue{mdf@leftline}\boolfalse{mdf@rightline}\else\boolfalse{mdf@leftline}\booltrue{mdf@rightline}\fi}

should provide the relevant 'mapping'. The settings code is executed as a hook at the start of mdframed.

\documentclass[twoside]{book}
    \usepackage[T1]{fontenc}
    \usepackage[utf8]{inputenc}
    \usepackage{showframe}
    \usepackage{lipsum}

\usepackage[framemethod=tikz]{mdframed}
\makeatletter
\mdfdefinestyle{box}{% common style
    topline=false, bottomline=false,
    rightline=false,
    linewidth=2pt,
    backgroundcolor=none,
    settings={\ifodd\value{page}\booltrue{mdf@leftline}\boolfalse{mdf@rightline}\else\boolfalse{mdf@leftline}\booltrue{mdf@rightline}\fi},
apptotikzsetting={\tikzset{mdfbackground/.append style={%
                  fill=teal!40, fill opacity=0.25}}},
    frametitlefont=\sffamily\bfseries\color{black},
    splittopskip=1mm,
    frametitlebelowskip=0mm,
}
\makeatother
% Example
\mdtheorem[style=box,linecolor=teal!70
           ]{example}{Example}[chapter]%section

\begin{document}
\chapter{Test}
    \lipsum[2]    
\begin{example}[cross correlation]
\lipsum[2]
\end{example}

\newpage
    \lipsum[1-2]    
\begin{example}[cross correlation]
\lipsum[2]
\end{example}

\end{document}
  • Its work as I wish! Thank you very much! Now I can ask for further help: how to manage positions of box title. I must confess, that I'm lost in mdframed documentation ... all examples in it doesn't work. Seems I encounter some error in copying them to my editor (WinEdit). – Zarko Mar 4 '16 at 21:21
  • The solution use \value{page} for indication if box is on odd page. This would work in normal circumstances, however I'm afraid that in large documet this can fail. I sow, that for more reliable solution is suggested to use \pageref of box position. Is it possible to adopt your solution to this? – Zarko Mar 4 '16 at 21:37
  • @Zarko: It might be possible, I would use \getpagerefnumber then, because you need something to be expandable (which is not the case for \pageref.) You might have a look on tcolorbox, which has support for even/odd page, as far as I know – user31729 Mar 4 '16 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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