marcodaniel / mdframed

auto-split frame environment for LaTeX
http://marcodaniel.github.com/mdframed
66 stars 11 forks source link

Bug: argument "endcode" not working #40

Open tosti007 opened 2 years ago

tosti007 commented 2 years ago

According to the documentation there is both a endinnercode and endcode hook. The endinnercode works as expected and adds the given code right at the end inside the box. The endcode hook does not produce anything, it's simply ignored.

I have tried it on Overleaf with both Latex and pdfLatex compilers and with TeX Live versions 2014, 2017, and 2021.

The document used (copied and modified from the example tex file:

% arara: pdflatex
\documentclass{article}
\usepackage[tikz]{mdframed}
\usepackage{lipsum}

\begin{document}
\begin{mdframed}[%
  endinnercode={This is the endinnercode!},
  endcode={Here is the endcode!}]
 \lipsum 
\end{mdframed}
\lipsum

\end{document}
muzimuzhi commented 2 years ago

In the released mdframed.sty v1.9b (2013/07/01), an mdframed environment does

\color@begingroup
  \mdfsetup{userdefinedwidth=\linewidth,<mdf options>}%
  \mdf@startcode
  % ...
\color@endgroup
\mdf@endcode

One can see \mdf@endcode, the macro that holds the value of endcode, is set inside the color group, but is used outside that group. The combined effect is, the endcode passed to the optional argument of mdframed is never used.

Try this patch:

\documentclass{article}
\usepackage[tikz]{mdframed}
\usepackage{lipsum}

\usepackage{xpatch}
\makeatletter
% expand \mdf@endcode before the group ends
\xpatchcmd\endmdframed
  {\color@endgroup\mdf@endcode}
  {\expandafter\color@endgroup\mdf@endcode}
  {}{\PatchFailed}
\makeatother

\begin{document}
\begin{mdframed}[
  startinnercode={This is the startinnercode!},
  endinnercode={This is the endinnercode!},
  startcode={This is the startcode!},
  endcode={This is the endcode!}
]
 \lipsum[1][1-3]\par
\end{mdframed}

\end{document}

image