3

I don't know why, but when I put some sentences inside a tcolorbox with halign=center, if I change the font size (using lualatex), then the space between words is the one defined before the environment... So it looks very akward as I can't really change the font size inside:

enter image description here

\documentclass{article}
\usepackage{fontspec}
\usepackage{luatex85}
\usepackage[most]{tcolorbox}
\tcbuselibrary{raster,skins,breakable}

\begin{document}
\fontsize{50pt}{50pt}\selectfont
Here is a 50pt text.
\begin{tcolorbox}[halign=center]
  {\fontsize{10pt}{10pt}\selectfont
    Look my spacing.} % Too big spacing
  {\fontsize{20pt}{20pt}\selectfont
    And mine.} % Too big spacing
  {\fontsize{50pt}{50pt}\selectfont
    And mine.} % Good
\end{tcolorbox}

% Why does this line change the spacing, even if I use fontsize later?
\fontsize{10pt}{10pt}\selectfont
\begin{tcolorbox}[halign=center]
  {\fontsize{10pt}{10pt}\selectfont
    Look my spacing.} % Good
  {\fontsize{20pt}{20pt}\selectfont
    And mine.} % Too small spacing
  {\fontsize{50pt}{50pt}\selectfont
    And mine.} % Too small spacing
\end{tcolorbox}

\end{document}

Any idea what's the problem? I'd like to center the text, change the font size only inside the tcolorbox, and eventually be able to use several sizes in the same tcolorbox.

Thanks!

-- EDIT -- I found a solution with using \begin{center}...\end{center} instead of halign=center... but I still don't understand what's wrong with halign=center. I'm asking because for now a lua script is generating some latex code, and it's actually uses halign=center.

2 Answers 2

2

tcolorbox documentation (page 30) suggests to use halign=flush center option instead of halign=center. It also redirects to the corresponding explanation in pages 234 to 238 in pgfmanual.

\documentclass{article}
\usepackage{fontspec}
\usepackage{luatex85}
\usepackage[most]{tcolorbox}
\tcbuselibrary{raster,skins,breakable}

\begin{document}
\begin{tcolorbox}
  {\fontsize{10pt}{10pt}\selectfont
    Look my spacing.} % Too big spacing
  {\fontsize{20pt}{20pt}\selectfont
    A larger font should show a larger spacing.} % Too big spacing
  {\fontsize{50pt}{50pt}\selectfont
    And even a larger one if font size is larger.} % Good
\end{tcolorbox}

\begin{tcolorbox}[halign=center]
  {\fontsize{10pt}{10pt}\selectfont
    Look my spacing.} % Too big spacing
  {\fontsize{20pt}{20pt}\selectfont
    A larger font should show a larger spacing.} % Too big spacing
  {\fontsize{50pt}{50pt}\selectfont
    And even a larger one if font size is larger.} % Good
\end{tcolorbox}

\begin{tcolorbox}[halign=flush center]
  {\fontsize{10pt}{10pt}\selectfont
    Look my spacing.} % Too big spacing
  {\fontsize{20pt}{20pt}\selectfont
    A larger font should show a larger spacing.} % Too big spacing
  {\fontsize{50pt}{50pt}\selectfont
    And even a larger one if font size is larger.} % Good
\end{tcolorbox}
\end{document}

enter image description here

2

I have the same issue you faced. You can easily solve it by replacing halign=center with before upper*=\centering in the respective box options.

enter image description here

\documentclass{article}
\usepackage{fontspec}
\usepackage{luatex85}
\usepackage[most]{tcolorbox}
\tcbuselibrary{raster,skins,breakable}

\begin{document}
    \fontsize{50pt}{50pt}\selectfont
    Here is a 50pt text.
    \begin{tcolorbox}[before upper*=\centering]
        {\fontsize{10pt}{10pt}\selectfont
            Look my spacing.}\\
        {\fontsize{20pt}{20pt}\selectfont
            And mine.}\\
        {\fontsize{50pt}{50pt}\selectfont
            And mine.}
    \end{tcolorbox}
\end{document}

You must log in to answer this question.

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