4

I am using thmtools to create exercise environments. The format I am looking for is:

  • The exercise number, in bold
  • a period and space
  • If the exercise note is set (with the name) key, the note in bold and another space
  • the exercise body

The trouble I'm having is that I'm getting some extra horizontal spacing when the note is set. Also, if there is an empty block of options, more extra space is inserted. Here is the MWE:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools}

\declaretheoremstyle[
    headfont=\bfseries,
    notefont=\bfseries,
    headformat={\NUMBER.\if\NOTE\ \else \NOTE\fi},
    headpunct={},
    notebraces={}{},
    numbered=yes
]{exercise}
\declaretheorem[style=exercise]{exercise}

\begin{document}

\begin{exercise}foo\end{exercise}
\begin{exercise}[
    ] bar 
\end{exercise}
\begin{exercise}[name=Baz]baz\end{exercise}

\end{document}

MWE output

Without the headformat key set I don't get any of these extra spaces, but them of course it's not the format I want. So how do I set headformat to get what I want without the extra space?

  • 1
    Add \show\NOTE to your headformat definition (somewhere) and see what \NOTE looks like. It doesn't just contain the note, but is comprised of an \if..\fi, so your test wouldn't work. – Werner Jun 17 '14 at 15:32
  • Good point. I think I see why I'm getting so much extra space in the second case. If there is a nonkeyed optional argument it is taken as an argument to the name key. So the second case is functionally equivalent to name={ }. Or something like that. – Matthew Leingang Jun 17 '14 at 15:52
4

\NOTE doesn't contain just the note text, but it is a more complex macro.

You get what you want with this:

\documentclass{article}
\usepackage{amsthm}
\usepackage{thmtools,etoolbox}

\declaretheoremstyle[
    headfont=\bfseries,
    notefont=\bfseries,
    headformat={\NUMBER.\NOTEifnotblank},
    headpunct={},
    notebraces={}{},
    numbered=yes
]{exercise}
\declaretheorem[style=exercise]{exercise}

\makeatletter
\newcommand{\NOTEifnotblank}{%
  \expandafter\getreal@NOTE\NOTE\@nil
}
\def\getreal@NOTE\if=#1=#2\@nil{\ifblank{#1}{}{\unskip\textnormal{\ }#1}}
%% fix the error if the optional argument is specified but empty
\let\thmt@shortoptarg\@empty
\makeatother

\begin{document}

\begin{exercise}
foo
\end{exercise}
\begin{exercise}[ ]
bar 
\end{exercise}
\begin{exercise}[]
bar 
\end{exercise}
\begin{exercise}[name=Baz]
baz
\end{exercise}

\end{document}

enter image description here

1

in the file thm-amsthm.sty, the formatting instruction for the NOTE doesn't account for the possibility of an "empty" option, whether that means just brackets [] in which case it crashes with the message

! Undefined control sequence.
\ll@exercise ...mtformatoptarg {\thmt@shortoptarg 
                                              }\fi 
l.22 \begin{exercise}[]

or with spaces as you have it.

a patch can be applied to get rid of unwanted spaces. in the definition

\def\thmt@setheadstyle#1{%

replace the line

\def\NOTE{\if=##3=\else\bgroup\thmt@space\the\thm@notefont(##3)\egroup\fi}%

with

\def\NOTE{\if=##3=\else\bgroup\thmt@space\the\thm@notefont(\ignorespaces##3)\egroup\fi}%

the \ignorespaces will do the trick.

  • Thanks for looking into this. I redefined the macro, and it does cut down on the extra space in the second cases, but there is still some there in the second and third cases. I am guessing Werner's comment needs to be incorporated too. – Matthew Leingang Jun 17 '14 at 15:47

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.