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.

The following code isn't very minimal but I hope you will be able to understand the code.

\setcounter{errorcontextlines}{999}
\documentclass{scrartcl}
\usepackage{lipsum,ntheorem}
\usepackage[ntheorem]{mdframed}
\makeatletter
%\newtheorem{<envname>}[<numberedlike>]{<caption>}[<within>]
%\newmdtheoremenv[<mdframed-options>]{<envname>}[<numberedlike>]{<caption>}[<within>]
%\md@tempa[<mdframed-options>]
%\md@tempb{<envname>}%
%\md@tempc[<numberedlike>]%
%\md@tempd{<caption>}%
%\md@tempe[<within>]%
\newrobustcmd*\newmdtheoremenv{%
   \kernel@ifnextchar[%]
     {\newmdtheoremenv@i}{\newmdtheoremenv@i[]}%
}
\newrobustcmd*\newmdtheoremenv@i[2][]{%
\def\md@tempa{#1}%
\def\md@tempb{#2}%
    \kernel@ifnextchar[%]
     {\newmdtheoremenv@ii}{\newmdtheoremenv@ii[]}%
}

%\newmdtheoremenv@ii[<mdframed-options>]{<envname>}[<numberedlike>]{<caption>}
\newrobustcmd*\newmdtheoremenv@ii[2][]{%
\def\md@tempc{#1}%
\def\md@tempd{#2}%
    \kernel@ifnextchar[%]
     {\newmdtheoremenv@iii}{\newmdtheoremenv@iii[]}%
}

%\newmdtheoremenv@ii[<mdframed-options>]{<envname>}[<numberedlike>]{<caption>}[<within>]
\newrobustcmd*\newmdtheoremenv@iii[1][]{%
   \def\md@tempe{#1}%
   \ifboolexpr{ not (test {\ifdefempty{\md@tempc}})
              and 
             not (test {\ifdefempty{\md@tempe}})
           }{%
             \md@PackageWarning{You defined {\string\newtheorem} with ^^J
                                both optional arguments^^J 
                                <numeredlike> and <within>. This isn't allowed^^J
                                \mdframedpackagename will use <numeredlike>\MessageBreak}%
             \newtheorem{\csname md\md@tempb \endcsname}[\md@tempc]{\md@tempd}%
            }%
            {\ifdefempty{\md@tempc}{}{\newtheorem{md\md@tempb}[\md@tempc]{\md@tempd}}%
             \ifdefempty{\md@tempe}{}{\newtheorem{md\md@tempb}{\md@tempd}[\md@tempe]}%
            }%
   \ifboolexpr{test {\ifdefempty{\md@tempc}}
                and 
               test {\ifdefempty{\md@tempe}}
               }{%
               \newtheorem{md\md@tempb}{\md@tempd}%
               }{}%
\expandafter\global\expandafter\@namedef\expandafter{\md@tempb}{%
   \expandafter\mdfsetup\expandafter{\md@tempa}%
               \begin{mdframed}[]\begin{md\md@tempb}%
    }%
\expandafter\global\expandafter\@namedef\expandafter{end\md@tempb}{%
              \end{md\md@tempb}\end{mdframed}%
   }%
}
\makeatother
\begin{document}
{\small\verb+\newmdtheoremenv[mdframed-options]{envname}[numberedlike]{caption}[within]+}
\newmdtheoremenv[linecolor=blue,backgroundcolor=red]{beispiel}{Beispiel}
%\newmdtheoremenv[linecolor=red,backgroundcolor=yellow]{spiel}{Spiel}
\begin{beispiel}[Antwort]
Test
\end{beispiel}

\vspace*{2em}

%\hallo

\vspace*{2em}

%\begin{spiel}%[Antwort]
%Test
%\end{spiel}
\end{document}

I am trying to create the command \newmdtheoremenv. In the minimal example it works well as long as only one \newmdtheoremenv is used. When I use the second \newmdtheoremenv the first will be overwritten. How can I avoid this and how can I modify the example so that the command \newmdtheoremenv can be used multiple times.

share|improve this question
1  
On a general note: Only the first macro at the user level need to be robust. Also you check for [ manually but then use macros which already has an optional arguments and repeat the exact same by themselves! So change \newrobustcmd*\newmdtheoremenv@i[2][] with a simple \def\newmdtheoremenv@[#1]#2 or remove the \kernel@ifnextchar[ code. Only one is required; both together don't make much sense. –  Martin Scharrer Aug 22 '11 at 17:35

2 Answers 2

up vote 3 down vote accepted

Unfortunately Martin's answer doesn't work, because the macros \md@tempa and similar are not expanded at definition time and so all theorems will have the same label as the last defined one.

\setcounter{errorcontextlines}{999}
\documentclass{scrartcl}
\usepackage{lipsum,ntheorem}
\usepackage[ntheorem]{mdframed}
\makeatletter
%\newtheorem{<envname>}[<numberedlike>]{<caption>}[<within>]
%\newmdtheoremenv[<mdframed-options>]{<envname>}[<numberedlike>]{<caption>}[<within>]
%\md@tempa[<mdframed-options>]
%\md@tempb{<envname>}%
%\md@tempc[<numberedlike>]%
%\md@tempd{<caption>}%
%\md@tempe[<within>]%
\renewrobustcmd*\newmdtheoremenv{%
   \kernel@ifnextchar[%]
     {\newmdtheoremenv@i}{\newmdtheoremenv@i[]}%
}
\def\newmdtheoremenv@i[#1]#2{%
    \def\md@tempa{#1}%
    \def\md@tempb{#2}%
    \kernel@ifnextchar[%]
     {\newmdtheoremenv@ii}{\newmdtheoremenv@ii[]}%
}

\def\newmdtheoremenv@ii[#1]#2{%
    \def\md@tempc{#1}%
    \def\md@tempd{#2}%
    \kernel@ifnextchar[%]
     {\newmdtheoremenv@iii}{\newmdtheoremenv@iii[]}%
}

\def\newmdtheoremenv@iii[#1]{%
   \def\md@tempe{#1}%
   \ifboolexpr{ not (test {\ifdefempty{\md@tempc}})
              and 
             not (test {\ifdefempty{\md@tempe}})
           }{%
             \md@PackageWarning{You defined {\string\newtheorem} with ^^J
                                both optional arguments^^J 
                                <numberedlike> and <within>. This isn't allowed^^J
                                \mdframedpackagename will use <numeredlike>\MessageBreak}%
             \begingroup\edef\x{\endgroup
               \noexpand\newtheorem{\csname md\md@tempb\endcsname}[\md@tempc]{\md@tempd}}\x
            }%

            {\ifdefempty{\md@tempc}{}{
               \begingroup\edef\x{\endgroup
                 \noexpand\newtheorem{md\md@tempb}[\md@tempc]{\md@tempd}}\x
}%
             \ifdefempty{\md@tempe}{}{
               \begingroup\edef\x{\endgroup
                 \noexpand\newtheorem{md\md@tempb}{\md@tempd}[\md@tempe]}\x
}%
            }%
   \ifboolexpr{test {\ifdefempty{\md@tempc}}
                and 
               test {\ifdefempty{\md@tempe}}
               }{%
                 \begingroup\edef\x{\endgroup
                   \noexpand\newtheorem{md\md@tempb}{\md@tempd}}\x
               }{}%
   \expandafter\protected@xdef\csname\md@tempb\endcsname{\noexpand\mdfsetup{\md@tempa}%
               \noexpand\begin{mdframed}[]\noexpand\begin{md\md@tempb}%
    }%
   \expandafter\protected@xdef\csname end\md@tempb\endcsname{%
              \noexpand\end{md\md@tempb}\noexpand\end{mdframed}%
   }%
}
\makeatother
\begin{document}
{\small\verb+\newmdtheoremenv[mdframed-options]{envname}[numberedlike]{caption}[within]+}
\newmdtheoremenv[linecolor=blue,backgroundcolor=red]{beispiel}[section]{Beispiel}
\newmdtheoremenv[linecolor=red,backgroundcolor=yellow]{spiel}[subsection]{Spiel}

\begin{beispiel}[Antwort]
Test
\end{beispiel}

\vspace*{2em}

\begin{spiel}%[Antwort]
Test
\end{spiel}
\end{document}

NOTE

I've used \renewrobustcmd because the mechanism has already been included in my version of mdframed.

share|improve this answer

You currently define the environments so that they use the temporary macros which are getting overwritten at every new declaration. You need to properly expand them first. Also you don't need to expand the argument of \@namedef because it is anyway expanded by the underlying \csname .. \endcsname.

\setcounter{errorcontextlines}{999}
\documentclass{scrartcl}
\usepackage{lipsum,ntheorem}
\usepackage[ntheorem]{mdframed}
\makeatletter
%\newtheorem{<envname>}[<numberedlike>]{<caption>}[<within>]
%\newmdtheoremenv[<mdframed-options>]{<envname>}[<numberedlike>]{<caption>}[<within>]
%\md@tempa[<mdframed-options>]
%\md@tempb{<envname>}%
%\md@tempc[<numberedlike>]%
%\md@tempd{<caption>}%
%\md@tempe[<within>]%
\newrobustcmd*\newmdtheoremenv{%
   \kernel@ifnextchar[%]
     {\newmdtheoremenv@i}{\newmdtheoremenv@i[]}%
}
\def\newmdtheoremenv@i[#1]#2{%
    \def\md@tempa{#1}%
    \def\md@tempb{#2}%
    \kernel@ifnextchar[%]
     {\newmdtheoremenv@ii}{\newmdtheoremenv@ii[]}%
}

\def\newmdtheoremenv@ii[#1]#2{%
    \def\md@tempc{#1}%
    \def\md@tempd{#2}%
    \kernel@ifnextchar[%]
     {\newmdtheoremenv@iii}{\newmdtheoremenv@iii[]}%
}

\def\newmdtheoremenv@iii[#1]{%
   \def\md@tempe{#1}%
   \ifboolexpr{ not (test {\ifdefempty{\md@tempc}})
              and 
             not (test {\ifdefempty{\md@tempe}})
           }{%
             \md@PackageWarning{You defined {\string\newtheorem} with ^^J
                                both optional arguments^^J 
                                <numeredlike> and <within>. This isn't allowed^^J
                                \mdframedpackagename will use <numeredlike>\MessageBreak}%
             \newtheorem{\csname md\md@tempb \endcsname}[\md@tempc]{\md@tempd}%
            }%
            {\ifdefempty{\md@tempc}{}{\newtheorem{md\md@tempb}[\md@tempc]{\md@tempd}}%
             \ifdefempty{\md@tempe}{}{\newtheorem{md\md@tempb}{\md@tempd}[\md@tempe]}%
            }%
   \ifboolexpr{test {\ifdefempty{\md@tempc}}
                and 
               test {\ifdefempty{\md@tempe}}
               }{%
               \newtheorem{md\md@tempb}{\md@tempd}%
               }{}%
   \expandafter\protected@xdef\csname\md@tempb\endcsname{\noexpand\mdfsetup{\md@tempa}%
               \noexpand\begin{mdframed}[]\noexpand\begin{md\md@tempb}%
    }%
   \expandafter\protected@xdef\csname end\md@tempb\endcsname{%
              \noexpand\end{md\md@tempb}\noexpand\end{mdframed}%
   }%
}
\makeatother
\begin{document}
{\small\verb+\newmdtheoremenv[mdframed-options]{envname}[numberedlike]{caption}[within]+}
\newmdtheoremenv[linecolor=blue,backgroundcolor=red]{beispiel}{Beispiel}
\newmdtheoremenv[linecolor=red,backgroundcolor=yellow]{spiel}{Spiel}

\begin{beispiel}[Antwort]
Test
\end{beispiel}

\vspace*{2em}

\begin{spiel}%[Antwort]
Test
\end{spiel}
\end{document}
share|improve this answer
    
Thanks. I will use also the important information in the comment above. –  Marco Daniel Aug 22 '11 at 17:58

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.