2

I've run into undesirable behavior with tcolorbox that I cannot understand, and am hoping someone here can give insight into what's going on. If I compile the following (for me using pdflatex 2023) then there is unwanted spacing in the 3rd row.

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{raster}

\newtcolorbox{mybox}[1]{%
attach title to upper,%
before title={\hspace{-5ex}\makebox[5ex]{#1.}},%
title={\notblank{#1}{}{}}%
}%

\begin{document}%

\begin{tcbraster}[%
raster left skip=0.05\linewidth%
]%
\begin{mybox}{1}%
\(1+2=3\)%
\end{mybox}%
\begin{mybox}{2}%
\(1+2=3\)%
\end{mybox}%
\begin{mybox}{3}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{4}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{5}%
\({12\left(345-678\right)-90\left(123-45\right)}\)%
\end{mybox}%
\begin{mybox}{6}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{7}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{8}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\end{tcbraster}%
\end{document}

This is a MWE so the code might seem strange, but I whittled down my real example to this. Any time the content of one of these boxes is just a bit too large, like the first box in the 3rd row, there is this extra indenting. Also there is a small amount of vertical space added there. You can see this here:

enter image description here

If I comment out any one of the three options to the \newtcolorbox, the unwanted space goes away. If I comment out the one option to the tcbraster environment, that also makes the unwanted space go away. So it seems that it's not any one of those options; it's all of them in combination.

Also I find this strange: if I replace title={\notblank{#1}{}{}} with title={} or title={{}}, that also makes the unwanted space go away. This suggests that \notblank{}{}{} does not work in the way I would expect.

Can anyone explain what introduces the extra space? Or how I can prevent this with global tcbsettings? (I know with this particular example I could change some of the widths that are used here, but in general I cannot predict the width of the content in each box and I am hoping to find a solution that is agnostic to the particulars of any width.)


Added later. Here is a less minimal example. The application produces a numbered list of math exercises. Some of them have titles. After bringing back some of the things I previously removed for the MWE, I have to make the troublesome 5th box a bit larger to trigger the issue. The 4th box with a faux title also now demonstrates the issue.enter image description here

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{raster}
\tcbuselibrary{breakable}
\tcbuselibrary{hooks}

\newtcolorbox{mybox}[2]{
size=minimal,
boxrule=-0.3pt,
frame empty,
colback=white,
colbacktitle=white,
coltitle=black,
opacityfill=0.0,
fonttitle=\upshape\bfseries, attach title to upper,
before skip={1.5ex plus 0.5ex},
left=5ex,
halign=flush left,
unbreakable,
before title={\hspace{-5ex}\makebox[5ex][l]{#1.}},
title={\notblank{#2}{#2. }{}},
}


\begin{document}%
\begin{tcbraster}[%
raster left skip=0.05\linewidth%
]%
\begin{mybox}{1}{Title}%
\(1+2=3\)%
\end{mybox}%
\begin{mybox}{2}{}%
\(1+2=3\)%
\end{mybox}%
\begin{mybox}{3}{}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{4}{Title}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{5}{}%
\({12\left(345-678\right)-90\left(123-4567\right)}\)%
\end{mybox}%
\begin{mybox}{6}{}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{7}{}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{8}{}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\end{tcbraster}%
\end{document}
4
  • 1
    @AndréC The 5th box is intentionally one character more wide that surrounding boxes. It has a "5" at the end. This makes it just long enough to trigger this issue. Commented Jul 11 at 9:24
  • It seems that if I remove the braces that surround all of my math expressions, that may resolve my issue (pending more testing). Commented Jul 11 at 9:41
  • 1
    you need before title={\hspace*{-5ex}\makebox[5ex]{#1.}},%, to avoid that you loose the negative space sometimes. Commented Jul 11 at 10:52
  • @UlrikeFischer Thanks, I think this is the answer. Would you like to make it an answer that I can mark accepted? Commented Jul 12 at 17:59

3 Answers 3

2

you need

before title={\hspace*{-5ex}\makebox[5ex]{#1.}},%

with a starred \hspace, to avoid that you loose the negative space in some cases.

1

I'm not sure what you're doing with titles, because \notblank{#1}{}{} will execute nothing at all.

Anyway, removing all unnecessary % and \left\right commands, I get no issue. You can see that removing all % from your code has no influence on the output. I'm certainly not saying that they're not useful and sometimes necessary; they aren't in the cases you used them.

Also \left and \right in this context just produces unwanted space.

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{raster}

\newtcolorbox{mybox}[1]{
  attach title to upper,
  before title={\hspace{-5ex}\makebox[5ex]{#1.}},
  title={\notblank{#1}{}{}},
}

\begin{document}

\begin{tcbraster}[
  raster left skip=0.05\linewidth
]
\begin{mybox}{1}
\(1+2=3\)
\end{mybox}
\begin{mybox}{2}
\(1+2=3\)
\end{mybox}
\begin{mybox}{3}
\(12(345-678)-90(123-4)\)
\end{mybox}
\begin{mybox}{4}
\(12(345-678)-90(123-4)\)
\end{mybox}
\begin{mybox}{5}
\(12(345-678)-90(123-45)\)
\end{mybox}
\begin{mybox}{6}
\(12(345-678)-90(123-4)\)
\end{mybox}
\begin{mybox}{7}
\(12(345-678)-90(123-4)\)
\end{mybox}
\begin{mybox}{8}
\(12(345-678)-90(123-4)\)
\end{mybox}
\end{tcbraster}

\end{document}

enter image description here

7
  • "I'm not sure what you're doing with titles"... well I pared down a real example to an MWE because I thought it would be helpful. Should I post a larger example? Commented Jul 11 at 9:07
  • And removing the \left and \right just makes the expression narrower, because the extra spacing added by \left and \right is absent. I know I don't have the issue if the content is narrower. Commented Jul 11 at 9:09
  • I could have been more clear, but this is not tex that is manually edited, so I can't do some small local adjustment to get past this issue when it happens. I'm working on software that produces big tex files to be compiled so I'm looking for a global tcb adjustments to combat this. Commented Jul 11 at 9:11
  • I added a more complete example that might explain what is going on with titles. Commented Jul 11 at 9:35
  • Sorry for the comment barrage, but about "\notblank{#1}{}{} will execute nothing at all". I would agree with that, except that it's not equivalent to just having {} there. If I take my original example and only replace \notblank{#1}{}{} with {}, I get a different result. That is part of the mystery I can't solve. Commented Jul 11 at 9:37
0

Have a look to this solution:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{raster}

\newtcolorbox{mybox}[1]{%
}%

\begin{document}%

\begin{tcbraster}[%
raster left skip=0.05\linewidth%
]%
\begin{mybox}{1}%
\(1+2=3\)%
\end{mybox}%
\begin{mybox}{2}%
\(1+2=3\)%
\end{mybox}%
\begin{mybox}{3}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{4}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{5}%
\({12\left(345-678\right)-90\left(123-45\right)}\)%
\end{mybox}%
\begin{mybox}{6}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{7}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\begin{mybox}{8}%
\({12\left(345-678\right)-90\left(123-4\right)}\)%
\end{mybox}%
\end{tcbraster}%
\end{document}

What generates the following: enter image description here

What did I change? I removed the following lines from the mybox definition:

attach title to upper,%
before title={\hspace{-5ex}\makebox[5ex]{#1.}},%
title={\notblank{#1}{}{}}

These lines were responsible for adding a title to each box, which included the number in the top-left corner. By removing them, the title is no longer displayed, and the numbers are gone.

In particular:

  • attach title to upper was adding a title to the top of each box.
  • before title={\hspace{-5ex}\makebox[5ex]{#1.}} was adding a horizontal space and a box with the number #1. before the title.
  • title={\notblank{#1}{}{}} was setting the title to be the number #1 if it was not blank, and empty otherwise.

By removing these lines, the title is no longer displayed, and the numbers are no longer shown.

1
  • 1
    I'm sorry, my attempt to make an MWE has misled people (both you and egreg) to think I am free to remove those frivolous lines. Those lines have more meaningful content in my real example and I cannot remove them. I just pared them down to eliminate their content as being relevant. Commented Jul 11 at 9:13

You must log in to answer this question.

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