[Tex/LaTex] tcolorbox: vertical spacing in a tcolorbox

spacingtcolorbox

How can I obtain the same vertical spacing above and below the line of text in a tcolorbox?

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcolorbox{questions}{
  colframe = red
}
\begin{document}
\begin{questions}
  \centering
  What is the spatial period?
\end{questions}
\end{document}

enter image description here

From the image, we can see that the spacing below the sentence is more than above the spacing. I tried using \vfill but nothing changed.

Also, as an additional question, can I stop indentations after a tcolorbox when a new sentence is started?

Best Answer

Looks can be deceiving. Because of the "p" descending letters, the vertical gap from the bottom of the "p" to the box is the same as the top of the "W" to the box (see second image). Try it with the p's removed (or \smashed as I show in the third image of this MWE). The gap is identical relative, not to the text baselines, but with respect to the actual bottom and top of the text box (second and fourth images).

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcolorbox{questions}{
  colframe = red
}
\begin{document}
\begin{questions}
  \centering
  What is the spatial period?
\end{questions}
\begin{questions}
  \centering
  \fboxsep=0pt\fbox{What is the spatial period?}
\end{questions}
\begin{questions}
  \centering
  What is the s\smash{p}atial \smash{p}eriod?
\end{questions}
\begin{questions}
  \centering
  \fboxsep=0pt\fbox{What is the s\smash{p}atial \smash{p}eriod?}
\end{questions}
\end{document}

enter image description here

As far as the indent,

\newtcolorbox{questions}{
  nobeforeafter,
  colframe = red
}

will prevent the following text from being indented. However, it will also have the effect of allowing the tcolorboxes to be indented, which you may not want. Furthermore, it will change/eliminate the padding around the box.

As an alternative, a \noindent can manually override indentation on the subsequent text.