1

Problems:

Extra alignment tab has been changed to \cr.

Which I think means something like the LaTeX code has a different amount of cells in this row than what was defined in the tabularx premable.

The issue doesn't exist until I attempt to add more rows after the header, but the last cell on the right is sort of...elevated? I don't quite understand the problem here, I mean in a tabularx environment you separate cells by & and end a row by // , right?

Also, if you change the last header title to: {\centering T} it doesn't crash or elevate the text but...it doesn't center the text.

Any ideas?

NOTES: The bigger picture here is that I want to align the header differently than the cells. So I'm trying to just use \centering on the header. \begin{center} does it but puts a ton of space around it.

MWE:

\documentclass[11pt]{letter}                              
\usepackage[letterpaper, inner=18mm, outer=18mm, top=8mm, bottom=25mm]{geometry}
\usepackage{tabularx}                                               
\usepackage[table,xcdraw]{xcolor}
    
\renewcommand{\tabularxcolumn}[1]{>{\tiny}m{#1}}
\newcolumntype{x}{>{\raggedleft\arraybackslash}X}  
        
\begin{document}

    \rowcolors{1}{}{gray!15}
        \begin{tabularx}{\textwidth}{xxxxxxxxxxxx}
            \rowcolor{gray}

    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &          
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering text &   
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text &   
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &  
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text &   
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &  
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &  
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &  
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text Text Text &     
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &  
    \color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &  
    \color{white}\fontsize{6pt}{6pt}\selectfont {\centering Tt} \\  

    \color{black}\fontsize{6pt}{6pt}\selectfont 8'  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 130  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 134  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 152  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 168  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 200  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 240  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 340  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 520  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 340  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont 520  & 
    \color{black}\fontsize{6pt}{6pt}\selectfont -  \\

\end{tabularx}
\end{document}

Edit:

Firstly, it may seem really inconvenient how I'm repeating a lot of things but really I'm just typing it once and the computer is repeating it in a loop.

I haven't been using multicolumn because the headers extend past the table cells widths and I cannot define a static width for each cell (or I wouldn't have a problem) because in the BIGGER picture I'm templating the LaTeX file with Scriban and I'm dealing with a dynamic header. Here is what happens when using multicolumn:

enter image description here

I was able to center the headers with \begin{center} but the space around each header cell was undesirable, so I looked it up and tried a new environment here: How can I change the whitespace above and below center? which was able to remove the space above, but not below. I placed the \begin and \end{center} right before and after each header cell. Here is what that looks like:

enter image description here

I don't need tiny cells, I just thought that would change the width multicolumn forcing for the headers.

I was just puzzled because tabular is reacting weirdly to the \centering modifier in each line.

Also, I am using tabularX because it extends it dynamically to the end of the page.

6
  • You can use \multicolumn{1}{c}{...} in order to horizontally center the text of a single cell. An alternative would be the \thead command from the makecell package.
    – leandriis
    Jul 1, 2020 at 21:08
  • Could you please add some background information on the other contents of the table? Will you need linebreaks inside of table cells? Also, why do you need all teh repeated \color{black}\fontsize{6pt}{6pt}\selectfont? Why do you need a \tiny font size here?
    – leandriis
    Jul 1, 2020 at 21:11
  • you could use \centering\arraybackslash just as you used \raggedright\arraybackslash but there is no line breaking within the cells so you shouldn't be using tabularx here, just use tabular Jul 1, 2020 at 21:45
  • also you do not need to specify the color and font size in every cell, just specify them once before the table Jul 1, 2020 at 21:47
  • @DavidCarlisle David to the rescue again. The answer is that the \arraybackslash needed to be right after \centering. How come that's the case, what does the \arraybackslash really do for tabular?
    – tblev
    Jul 2, 2020 at 14:15

2 Answers 2

1

After one year (I have obviously overlooked this issue before) ...

I would wrote your table very differently:

  • font size I would define before tabularx, by this all their repetitions in each cell become superfluous
  • I wonder if you really need so small fonts. From provided content of table can be concluded, that font size can be much large, for example equal to \small
  • for changing align column headers contents I would define new command:
    \newcommand\mcw[1]{\multicolumn{1}{C}{\textcolor{white}{#1}}}
    
    where C is defined as >{\centering\arraybackslash}X
  • I also would add small vertical spaces above cells contents by \setlength\extrarowheight{2pt}
  • after those measures code for table become much shorter and clear, consequently much less prone to errors in writing it:
\documentclass[11pt]{article}%{letter}
\usepackage[letterpaper, inner=18mm, outer=18mm, top=8mm, bottom=25mm]{geometry}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[table, xcdraw]{xcolor}
\newcommand\mcw[1]{\multicolumn{1}{C}{\textcolor{white}{#1}}}

\begin{document}
    \begin{table}[ht]
    \small\linespread{0.84}\selectfont\relax%\fontsize{6pt}{6pt}\selectfont\relax
    \setlength\extrarowheight{2pt}
    \rowcolors{1}{}{gray!15}
 \begin{tabularx}{\textwidth}{*{12}{L}}
            \rowcolor{gray}
\mcw{Text} & \mcw{Text} & \mcw{Text} & \mcw{Text} & \mcw{Text} & \mcw{Text} &
\mcw{Text Text Text} 
           & \mcw{Text} & \mcw{Text} & \mcw{Text} & \mcw{Text} & \mcw{Tt}       \\
%
8'         & 130        & 134        & 152        & 168         & 200       & 
240        & 340        & 520        & 340        & 520         & -             \\
8'         & 130        & 134        & 152        & 168         & 200       &
240        & 340        & 520        & 340        & 520         & -             \\
8'         & 130        & 134        & 152        & 168         & 200       &
240        & 340        & 520        & 340        & 520         & -             \\
    \hline
\end{tabularx}
    \end{table}
\end{document}

enter image description here

0

Replace

\color{white}\fontsize{6pt}{6pt}\selectfont \centering Text  &

with

\color{white}\fontsize{6pt}{6pt}\selectfont \centering\arraybackslash Text  &

You must log in to answer this question.

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