0

I have a number of environments that have very similar definitions, one example of which is below.

\newcounter{definition}
\newenvironment{definition}[1][]{%
 \refstepcounter{definition}
 \global\mdfdefinestyle{definition}{linecolor=grey60, linewidth=3pt, skipabove=2\parskip,   skipbelow=0.4\parskip, topline=false, rightline=false, bottomline=false}
  \ifstrempty{#1}%
  {\begin{mdframed}[style=definition]
  {\bfseries{Definition~\thedefinition}\newline}
  \relax}
  {\begin{mdframed}[style=definition]
  {\bfseries{Definition~\thedefinition}} \,(#1)\newline\relax
}}{\end{mdframed}}
\renewcommand\thedefinition{\arabic{chapter}.\arabic{section}.\arabic{definition}}

The other environments are very similar to this but with theorem in place of each definition, for example. How might one define a 'skeleton' environment that takes the environment name and correctly defines a new environment to correspond with that (including changing \thedefinition to \thetheorem and Definition~ to Theorem~), but without relying on many other packages (I'm trying to learn how to write all my macros myself so that I don't have to rely too heavily on packages)?

In addition to this, is it a trivial extension to set it up so that I may define \linewidth and each of the box lines as arguments to this skeleton environment, so that the theorem environment has a larger left line and includes a right line too?

Of course, with only a handful of environments I could just define each of them separately as I have done already, but I would be very interested in seeing how this code would look in case there are a great deal of environments that can be altered globally at once. I am sure that this is a feature in the amsthm package for their default theorem style, but I don't know where to go to check the code that they implement (if someone could point me in the correct direction for that also, I would be very grateful!).

4
  • \global\mdfdefinestyle can't possibly be doing anything good, shouldn't that style definition just be done once, in the preamble not every time (and definitely never prefixed with \global) – David Carlisle Apr 1 '17 at 14:13
  • @DavidCarlisle I've included it since the definition changes with each environment type (thicker lines for different environments and such), and the \global prefix is admittedly just something I had copied from someone else --- I had considered removing it since I was unsure how relevant it is, but it hasn't yielded any errors (yet) so I kept it in. If you don't think it needs to be there, I'll get rid of it though! – Bill Wallis Apr 1 '17 at 14:17
  • but the style is specific to definition environment \mdfdefinestyle{definition}... so why are you including it in every use of that environment. It should be like the definition you have of \thedefininition just done once. – David Carlisle Apr 1 '17 at 14:24
  • \global never makes an error applies to the first non expandable token in the expansion of \mdfdefinestyle and then does nothing if that isn't a primitive assignment and makes that assignment global if it is. So whatever it does it does by accident:-) – David Carlisle Apr 1 '17 at 14:28
2

I'm not sure that all aspects of the original definition are correct (\global can not prefix mdfdefinestyle for example) but to parameterise it just use #1 for definition and \csname thedefinition\endcsname for \thedefinition, and ##1 for #1.

\newcommand\mythmdef[1]{%
\newcounter{#1}%
\newenvironment{#1}[1][]{%
 \refstepcounter{#1}%
 \global\mdfdefinestyle{#1}{linecolor=grey60, linewidth=3pt, skipabove=2\parskip,   skipbelow=0.4\parskip, topline=false, rightline=false, bottomline=false}%
  \ifstrempty{##1}%
  {\begin{mdframed}[style=#1]%
  {\bfseries{\MakeUppercase#1~\csname the#1\endcsname}\newline}%
  \relax}%
  {\begin{mdframed}[style=#1]%
  {\bfseries{\MakeUppercase#1~\csname the#1\endcsname}} \,(##1)\newline\relax
}}{\end{mdframed}}%
\expandafter\renewcommand\csname the#1\endcsname{\arabic{chapter}.\arabic{section}.\arabic{#1}}}

then you can go

\mythmdef{definition}
3
  • @ChristianHupfer missed one, will fix:-) – David Carlisle Apr 1 '17 at 14:28
  • This is perfect for the parameterisation I was after, thank you! As a side question, is it appropriate to comment off every line in a macro? I thought it was only for lines that would induce incorrect spacing and whatnot (unless that is all of the lines, I'm still learning). – Bill Wallis Apr 1 '17 at 14:37
  • @BillWallis I think all the % I added protect you from white space in the output, it's possible that some are not strictly needed but no point in analysing too deeply if in doubt add. it always saves memory. – David Carlisle Apr 1 '17 at 15:09

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.