2

Using PDFLaTeX, I am trying to equip theorem-like environments with a colored bar in the outer margin of the document. These theorems are already using the (nice) thmbox formatting. After trying the framed and then mdframe packages, I have found that the best solution to really use the document margin was the changebar package.

I tried to patch the commands defined for each theorem by using etoolbox to no avail, and after a bit of searching the documentation to regexpatch tells (in a rather complicated way --- for me) that this may be because of the optional first argument.

However, using regexpatch's xapptocmd in place of \apptocmd does not help. Here is my code.

\documentclass[12pt]{article}

\usepackage{thmbox}
\usepackage[pdftex,color,outerbars]{changebar}
\usepackage{regexpatch}

\newtheorem{definition}{Definition}

\xapptocmd{\definition}{\begin{changebar}}{}{\message{failure}}
\xpretocmd{\enddefinition}{\end{changebar}}{}{\message{failure}}


\begin{document}

\begin{definition}[Square]
Given any number $a$, the square of $a$ is the number
\[a^2=a\times a.\]
\end{definition}

\end{document}

Compiling this code gives me 5 errors, mainly about badly closed environments (\begin{definition} ended by \end{changebar} is the first error).

If I comment out the \usepackage{thmbox}, the compilation goes on but no changebar appears in the margin.

If I comment out the patching lines in the preamble and write in the document

\begin{definition}[Square]\begin{changebar}
Given any number $a$, the square of $a$ is the number
\[a^2=a\times a.\]
\end{changebar}\end{definition}

Then I get what I want... But I can't do that in every single definition I have in my real document.

What am I doing wrong?

2

Try this:

note that three (!) compilation steps seem to be required

I have the changebar environment enclosing the definition one because I did not read closely enough the OP, use:

\newenvironment{cdefinition}[1][]
    {\if\relax\detokenize{#1}\relax\begin{definition}\else
                      \begin{definition}[#1]\fi\begin{changebar}}
    {\end{changebar}\end{definition}}

for changebar inside rather than outside.

\documentclass[12pt]{article}

\usepackage{thmbox}
\usepackage[pdftex,color,outerbars]{changebar}

\newtheorem{definition}{Definition}

\newenvironment{cdefinition}[1][]
    {\begin{changebar}\if\relax\detokenize{#1}\relax\begin{definition}\else
                      \begin{definition}[#1]\fi}
    {\end{definition}\end{changebar}}


\begin{document}

  \begin{cdefinition}[Square]
    Given any number $a$, the square of $a$ is the number
    \[a^2=a\times a\;.\]
  \end{cdefinition}

another one:

  \begin{cdefinition}[Cube]
    Given any number $a$, the cube of $a$ is the number
    \[a^3=a\times (a\times a) = (a\times a)\times a\;.\]
  \end{cdefinition}

and another one:

  \begin{cdefinition}
    Given any non-zero number $a$, the inverse of $a$ is the number
    \[a^{-1}=\frac1a\;.\]
  \end{cdefinition}

\end{document}

enter image description here

alternative:

enter image description here

  • Thank you for your answer. Your alternative with the changebar "inside" the theorem is what I want, I would never have thought of using things as detokenize to test the argument. I guess this should go into the patching of the regexpatch, and it's still frustrating not understanding why it won't work although it claims it does. Is there a way to additionally keep the original theorem name ? I don't want to define a new environment name, I just want to be able to recompile my existing document with just an additional class option. Thanks again ! – Frédéric Delacroix Jan 8 '16 at 18:10
  • it surely is possible to keep the former environment name, e.g. just do \newtheorem{definitionbis}{Definition} and then use definition rather than \cdefinition in my code above, with naturally definitionbis rather than defintiion inside. As per patching why bother ? there can not be a general solution, any solution be it regexpatch or anything else puts constraints and what will work with it. One can always examine the source code of the things one wants to patch and act accordingly. IIn this case I didn't even bother check what thmbox does. – user4686 Jan 8 '16 at 19:39
  • as per the use of \detokenize for the safe emptiness check, as far as I know it was first promoted by @HeikoOberdiek. White space will make the conditional choose the false branch. – user4686 Jan 8 '16 at 19:45
  • puts constraints and what will work with it., I meant naturally puts constraints on what will work with it. – user4686 Jan 8 '16 at 19:46
  • I get your point about environment names and perhaps I should have been clearer in my initial question. Thing is that my environments definition, theorem and numerous such others already exist and are being used. I just want that, by adding a macro (which will end up in my custom package) such environments automatically get the colorbar in the margin. My ultimate goal is to write a command that enables me to say \givecolorbar{definition}{red} to automatically add a red bar to all definitions. – Frédéric Delacroix Jan 11 '16 at 11:19
1

Here is a simple solution with the framed option of ntheorem, based on framed. It can break across pages.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[x11names]{xcolor}
\usepackage{framed}
\usepackage[framed]{ntheorem}
\usepackage{lipsum}

\theoremprework{%
\def\FrameCommand{\hspace{-9pt}\vrule width 3pt \hspace{6pt}}
}%
\newframedtheorem{definition}{Definition}
\theoremprework{%
\def\FrameCommand{{\color{IndianRed3}{\hspace{-8pt}\vrule width 2pt \hspace{6pt}}}}
}
\newframedtheorem{thm}{Theorem}

\begin{document}
\def\FrameHeightAdjust{20pt}
\mbox{}
\vskip 15cm
\begin{definition}[Square]
  Given any number $a$, the square of $a$ is the number
  \[a^2=a\times a.\]
\end{definition}
\begin{thm}
  \lipsum[3]
\end{thm}

\end{document} 

enter image description here

  • Thanks for your help but unless I'm mistaken this left bar does not go into the document margin, it rather pushes the theorem content to the right. The framed documentation claims that it produces an "ordinary frame box (\fbox) with edge at margin", not into the margin. Moreover, I really want to keep the thmbox layout. – Frédéric Delacroix Jan 8 '16 at 18:15
  • You didn't mention that. I can manage to put it in the margin. B.t.w., as far as I know, the thmboxes do not break across pages? – Bernard Jan 8 '16 at 18:44
  • @Frédéric Delacroix: Please see my updated answer. I've added the geometry package with showframe option. You have full control on the distance between the leftbar and the left margin. – Bernard Jan 8 '16 at 18:57

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.