2
\newtcolorbox{boxProblem}[2][sidebyside=false, lower separated = true]{
colback=purple!5!white,
colframe=violet,
colupper=violet!50!black,
fontupper=\bfseries,
fonttitle=\bfseries,
label = problem #2,
title={#2},#1}

I'm trying to pass math as part of a title, like so:

\begin{boxProblem}{$(\exists n\in Z)(x^2 + y^2 = z^2)$}
    Description
\end{boxProblem}

but it breaks, specifically when the math has an equal sign. A previous question (math inside title in tcolorbox) asked something similar, and it was solved by putting braces around the title. This worked when hard coding the title, but not for passing it as a argument.

Any recommendations?

  • Welcome! You need to wrap each argument separately into $ signs. – user194703 Feb 11 '20 at 4:24
  • @Schrödinger'scat, I tried that and it made it slightly better but didn't fix it entirely. Before, it showed the section number on the first page along with part of my equation, before rendering the rest of the document on the next page(s). Now, it just shows the section number. It's still not ideal, but an improvement – Neil_P Feb 11 '20 at 4:25
  • Yes. You cannot pass this to label. To make the error go away, it suffices to replace label = problem #2, by label = problem, but this won't lead to uniquely labeled boxes. You could use a counter or another way to label them, e.g. by using a third argument. What do you prefer? – user194703 Feb 11 '20 at 4:55
3

I'm not sure what label=problem #2 is supposed to do, because you aren't numbering your problems. Even a “safe” title such as

\begin{boxProblem}{abc}
  Description
\end{boxProblem}

will produce nothing if \ref{problem abc} is used; only \pageref would print something sensible.

A label should only contain standard printable characters or underscores; other characters are actually allowed, but control sequences such as \exists definitely aren't. I'm inclined to think you wouldn't like to do something like

\pageref{problem $(\exists n\in Z)(x^2 + y^2 = z^2)$}

anyway. For instance, spaces would count and, even if that would be going to work,

\pageref{problem $(\exists n\in Z)(x^2+y^2=z^2)$}

would not refer to the stated problem.

A possible solution is to use the xparse library and add a (parenthesized) optional argument for a label when the title is not “label safe”.

\documentclass{article}
\usepackage[many]{tcolorbox}
\tcbuselibrary{xparse}

\NewTColorBox{boxProblem}{O{sidebyside=false, lower separated = true} m D(){#2}}{
  colback=purple!5!white,
  colframe=violet,
  colupper=violet!50!black,
  fontupper=\bfseries,
  fonttitle=\bfseries,
  label = {problem #3},
  title={#2},
  #1
}

\begin{document}

\begin{boxProblem}{abc}
  Description
\end{boxProblem}

\begin{boxProblem}{$(\exists n\in Z)(x^2 + y^2 = z^2)$}(FLT)
  Description
\end{boxProblem}

\pageref{problem abc}

\pageref{problem FLT}

\end{document}

enter image description here

The braces around {problem #3} protect against having = in the title or optional label argument.

Or just drop label=#2 altogether.

  • Thank you! Removing the label=#2 solved this. A little more on what I'm using this for: I have several environments: boxProblem, boxProof, boxDefinition, etc. Each of them have the title "Problem: #2" "Proof: #2", which is why I included the description. Some of the boxes (like the definitions and proofs) are numbered within the chapter, most are not. I copied and pasted the environments, which is why the label got transferred over. Thanks again! – Neil_P Feb 11 '20 at 14:13
  • @Schrödinger'scat What precisely did I get from your answer? – egreg Feb 11 '20 at 14:37
  • @Schrödinger'scat Sorry, I see nowhere in your comment or your answer the advice to drop label altogether. You are suggesting how to add it in a different way, which has the consequence to also ignore the default value of the optional argument (fact which you skip over without mention). You told about many things and mention one (wrong). Now, please, stop. – egreg Feb 11 '20 at 15:01
1

The original version of the question had the problem that the two arguments combined got wrapped into $ signs. The current version (while typing this) has the problem that you pass some math stuff to the label argument. You cannot do that (without further ado). If you want to keep the labels, and make them unique, one possibility is to use the auto counter for that. You can then use the optional argument to pass a label to the box.

\documentclass{article}
\usepackage{dsfont}
\usepackage{tcolorbox}
\newtcolorbox[auto counter]{boxProblem}[2][sidebyside=false, lower separated = true]{
colback=purple!5!white,
colframe=violet,
colupper=violet!50!black,
fontupper=\bfseries,
fonttitle=\bfseries,
title={#2},#1}
\begin{document}
\begin{boxProblem}[label=abc]{$(\exists n\in \mathds{Z})(x^n + y^n = z^n)$}
    Description abc
\end{boxProblem}

\begin{boxProblem}[label=xyz]{$(\exists m\in \mathds{Z})(x^n + y^n = z^n)$}
    Description xyz
\end{boxProblem}

Box~\ref{abc} deals with abc and box~\ref{xyz} with xyz.

\end{document}

enter image description here

Needless to say that there are many more possibilities, in particular in connection with cleveref and hyperref.

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.