0

I'd like to use tcolorbox to call a theorem environment. My tcolorbox is supposed to ask for an optional title, and a mandatory label, so in the xparse specification I chose O{}m. Then, in the definition I use

before upper={\begin{thm}[#1]\label{#2}},

which works great when the title is defined... but unfortunately when no title are defined this fails with error

Undefined control sequence.
\ll@thm ...t \thmtformatoptarg {\thmt@shortoptarg 
                                              }\fi 
l.54 \begin{theorem}{thm:mygreatlabel}

I guess because the thm environment does not accept an empty optional argument like \begin{thm}[] ... \end{thm}. I tried to add conditions, but the brackets are considered as real characters, so I don't know how to solve this problem.

Thanks!

MWE:

\documentclass{article}

\usepackage{mathtools}
\usepackage{amssymb, amsthm, amsmath}
\usepackage{thmtools} %%
\usepackage{hyperref}

\newtheorem{thm}{Theorem}[section]
\providecommand*\thmautorefname{Theorem}
\newtheorem{corollary}[thm]{Corollary}
\providecommand*\corollaryautorefname{Corollary}
\newtheorem{lemma}[thm]{Lemma}
\providecommand*\lemmaautorefname{Lemma}

\usepackage{lipsum}
\usepackage[most]{tcolorbox}

% \tcbset{customTheorem/.style={
% }

\NewTColorBox[auto counter, number within=section]{theorem}{O{}m}{ %
    blank,
    breakable,
    before upper={\begin{thm}[#1]\label{#2}},
      after upper={\end{thm}{\itshape See \hyperref[proof:#1]{proof} on page~\pageref{proof@\thetcbcounter}}},
    lowerbox=ignored,
    savelowerto=theorem-\thetcbcounter.tex,
    record={\string\myproof{\thetcbcounter}{theorem-\thetcbcounter.tex}{#2}},
% #1
}


\NewTotalTColorBox{\myproof}{mmm}{ %
  blank,
  breakable,
  before upper={\begin{proof}[Proof of \autoref{#3}]\label{proof:#3}},
    after upper={\end{proof}},
  phantomlabel={proof@#1},
}{\input{#2}}

% \tcbset{no proof/.style={no recording,after upper=\end{thm}}}

\begin{document}
\section{Some theorems}

\tcbstartrecording % [theoremsamazing.records]

\begin{theorem}[My title]{thm:mygreatlabel}
  This theorem works
  \tcblower
  Proof is super obvious
\end{theorem}

\begin{theorem}{thm:mygreatlabel}
  This fails because the environment thm does not accept empty brackets.
  \tcblower
  Proof is not super obvious
\end{theorem}

See, I can refer to \autoref{thm:mygreatlabel}!

\lipsum[2]
\tcbstoprecording

\section{Some amazing theorems}

\tcbinputrecords

\end{document}

1 Answer 1

2

The following definition of the theorem colorbox works:

\NewTColorBox[auto counter, number within=section]{theorem}{om}{ %
    blank,
    breakable,
    IfNoValueTF={#1}{before upper={\begin{thm}\label{#2}}}{before upper={\begin{thm}[#1]\label{#2}}},
      after upper={\end{thm}{\itshape See \hyperref[proof:#1]{proof} on page~\pageref{proof@\thetcbcounter}}},
    lowerbox=ignored,
    savelowerto=theorem-\thetcbcounter.tex,
    record={\string\myproof{\thetcbcounter}{theorem-\thetcbcounter.tex}{#2}},
% #1
}

Here, instead of optional argument with empty fallback O{} the optional argument without fallback o is used an IfNoValueTF differentiates cases of argument and no argument.

Notice that \begin{theorem}[]{label}...\end{theorem} with explicitly given empty optional argument still doesn't work.

4
  • Thanks, it work! So it's not possible to "factor" it without rewriting two times before upper?
    – tobiasBora
    Commented Apr 11, 2019 at 11:00
  • If it's possible then I don't know how, sorry. Commented Apr 11, 2019 at 11:09
  • Ok thanks. Can I also ask you if you know how to test also if the comment is empty or not? Like IfNoValueOrEmptyTF, but all the functions I found needed backslash that seems to be not recognized...
    – tobiasBora
    Commented Apr 11, 2019 at 12:24
  • Ok, I found the solution with before upper=\IfNoValueOrEmptyTF{#1}{...}{...}
    – tobiasBora
    Commented Apr 11, 2019 at 12:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .