2

I'm having a bit of trouble using typefaces like \textbf or \spacedlowsmallcaps in \makecells with line breaks. The following code works:

\begin{table}
    \centering
    \begin{tabular}{lcc}
        \toprule
        \textbf{HEADER 1} & \spacedlowsmallcaps{Header 2} & \spacedlowsmallcaps{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tabular}
\end{table}

If however, let's say the second header is very long and needs a line break and I want to use \makecell to get nice formatting, I get an error:

\begin{table}
    \centering
    \begin{tabular}{lcc}
        \toprule
        \textbf{HEADER 1} & \makecell[c]{\spacedlowsmallcaps{Very Long Header 2\\that needs a line break}} & \spacedlowsmallcaps{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tabular}
\end{table}

Here, LaTeX throws multiple instances of the following errors at me:

Missing } inserted.
  <inserted text>
Extra }, or forgotten \endgroup.
  <recently read> \egroup

Why is this and how do I fix it? I know I can just split the long text like

\spacedlowsmallcaps{Very Long Header 2}\\\spacedlowsmallcaps{that needs a line break}

But that makes the code much more cluttered and it doesn't work if I want to use it inside a newcommand where I just pass text that may or may not contain a line break.

EDIT 1: To be more clear: the same applies when I replace \spacedlowsmallcaps with something like \textbf or \textit.

EDIT 2: As requested, here is a full document illustrating the problem:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}

\newcommand{\tableheader}[2]{\makecell[#1]{\textbf{#2}}}

\begin{document}
\begin{table}
    \centering
    \begin{tabular}{lcc}
        \toprule
        % \textbf{HEADER 1} & \makecell[c]{\textbf{Very Long Header 2\\that needs a line break}} & \textbf{Header 3} \\
        % the above line does not work
        % \textbf{HEADER 1} & \makecell[c]{\textbf{Very Long Header 2}\\\textbf{that needs a line break}} & \textbf{Header 3} \\
        % this line is syntactically correct,
        % but it becomes a problem when using it with a predefined command:
        \tableheader{l}{HEADER 1} & \tableheader{c}{Very Long Header 2\\that needs a line break} & \tableheader{c}{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tabular}
\end{table}
\end{document}
6
  • 2
    PLease provide complete small document, which we can test as it is. Where is defined \spacedlowsmallcaps?
    – Zarko
    Commented Nov 16, 2022 at 14:36
  • It's from the classicthesis.sty which can be found at mirrors.ctan.org/macros/latex/contrib/classicthesis/…. The same applies for \textbf or \textit though.
    – Lithimlin
    Commented Nov 16, 2022 at 15:38
  • 1
    Please again, provide complete small document. Why should we google and make it if you already have it. Anyway, Test my answer if works. And see too, how this is "simple", if is provided only code fragment ... (which are surely correct)
    – Zarko
    Commented Nov 16, 2022 at 15:42
  • I provided a small example
    – Lithimlin
    Commented Nov 16, 2022 at 15:55
  • And what is the problem in this example? The same approach is considered in my answer ...
    – Zarko
    Commented Nov 16, 2022 at 15:58

2 Answers 2

3

Instead of reinventing the wheel, use the \thead macro which the makecell package provides for such cases:

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}

\renewcommand\theadfont{\bfseries}

\begin{document}
\begin{table}
    \centering
    \begin{tabular}{lcc}
        \toprule
         \thead{HEADER 1} & \thead[c]{Very Long Header 2\\that needs a line break} & \thead{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tabular}
\end{table}
\end{document}

enter image description here

3
  • Thanks for the reply. One quick question though: How would I change the font to the \spacedlowsmallcaps? I know I have to use \theadfont, but just setting it to this doesn't work. It is declared as: \DeclareRobustCommand{\spacedlowsmallcaps}[1]{\textls[80]{\ct@caps\MakeTextLowercase{#1}}} or \DeclareRobustCommand{\spacedlowsmallcaps}[1]{{\addfontfeatures{LetterSpace=14.0}\ct@caps\MakeTextLowercase{#1}}}
    – Lithimlin
    Commented Nov 16, 2022 at 17:15
  • @Lithimlin If your header is lowercase, you could use \renewcommand\theadfont{\slshape\scshape} Commented Nov 16, 2022 at 17:16
  • unfortunately, they aren't by default. I'd ave to be careful to always type them in lowercase
    – Lithimlin
    Commented Nov 16, 2022 at 17:21
3

Edit:
Using makcell each line which is formated with some command, as are \textbf{...} or \spacedlowsmallcaps{...} had to be closed before is line terminated. So,

\makecell[c]{\spacedlowsmallcaps{Very Long Header 2 \\
                                 that needs a line break}}

is wrong and

\makecell[c]{\spacedlowsmallcaps{Very Long Header 2}\\
             \spacedlowsmallcaps{that needs a line break}}

is correct.

According to above rule, your code fragment should be written as:

\begin{table}[ht]
    \centering
    \begin{tabular}{lcc}
        \toprule
        \textbf{HEADER 1} & \spacedlowsmallcaps{Header 2} & \spacedlowsmallcaps{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tabular}
\end{table}

\begin{table}[ht]
    \centering
    \begin{tabular}{lcc}
        \toprule
\textbf{HEADER 1} & \makecell[c]{\spacedlowsmallcaps{Very Long Header 2}\\ 
                                 \spacedlowsmallcaps{that needs a line break}}
                    & \spacedlowsmallcaps{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tabular}
\end{table}

\begin{table}[ht]
    \centering
    \begin{tblr}{colspec={lcc}, row{1}={m}}
        \toprule
\textbf{HEADER 1} & {\spacedlowsmallcaps{Very Long Header 2}\\
                     \spacedlowsmallcaps{that needs a line break}}
                    & \spacedlowsmallcaps{Header 3} \\
        \midrule
        cell 1 & cell 2 & cell 3 \\
    \end{tblr}
\end{table}

I will be able to complete provided code fragment when you will provide complete small document with your tables.

For the last table you need to load in preamble.

\usepackage{tabularray}
\UseTblrLibrary{booktabs}

Addendum:

  • At cases, when command for formatting text can be replaced by switch, for example \textbf{...} by \bfseries, you can write
\textbf{HEADER 1} & \bfseries\makecell[c]{Very Long Header 2\\
                                          that needs a line break}
                    & \textbf{Header 3} \\

which is equivalent to solution proposed in @samcarter_is_at_topanswers.xyz answer.

  • in similar cases the use of tabularry package can be very handy. For example, the example, which you add to question, can be written as:
\documentclass[10pt,a4paper]{article}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}[ht]
    \centering
\begin{tblr}{colspec={lcc},
             row{1} = {font=\bfseries, c, m}
             }
    \toprule
HEADER 1    &  {Very Long Header 2\\ that needs a line break}
                        & header 3  \\
    
    \midrule
cell 1      & cell 2    & cell 3    \\
cell 1      & cell 2    & cell 3    \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

enter image description here

10
  • Your proposed final solution does not compile for me. I get several errors in ninecolors.sty and cannot figure out where they're coming from. I tried moving around the import a bit but without any success.
    – Lithimlin
    Commented Nov 16, 2022 at 17:19
  • @Lithimlin, you have not loaded tabularray package. Load it.
    – Zarko
    Commented Nov 16, 2022 at 17:40
  • You mean like in lines 2 and 3? I have done that. That's what I meant when I said I tried moving around the imports.
    – Lithimlin
    Commented Nov 16, 2022 at 17:57
  • 1
    @Lithimlin, sory, than you change my suggested code and add some color into table . In such a case you should load in preamble xtabular package too.
    – Zarko
    Commented Nov 17, 2022 at 9:55
  • 1
    @Lithimlin, if now is all clarify, then is time to accept one from received answers (which you most liked). As I see, so far you not accept any answer on your question(s). For accešpting just click on check mark at top left side of selected answer.
    – Zarko
    Commented Nov 17, 2022 at 10:11

You must log in to answer this question.

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