1

I can see that relative column widths can be achieved via the tabularx package, as laid out in this question. I want my table to span the entire text width and to provide relative column widths to indicate how space should be allocated. However, I have specific formatting already applied to my table and I cannot reconcile the implementation of my formatting and the relative column widths.

Shown below is an image of a table already produced by my minimum working example, the code for which is also below. What I have now is a table which spans the full text width. It has a header row with specific text formatting and cell coloring. The subsequent rows have alternating colored backgrounds. As it is now, the column widths are simply set to be long enough to fit the data in the table. What I'd like is to combine the features of the tabularx package (or any other package/feature which accomplishes this goal) to say that the relative widths of the columns (for the example below) should be 0.15, 0.15, 0.55, and 0.15 respectively, while still retaining the rest of the table formatting. This way, the columns actually span the full text width. I cannot find a way to do this!


Current Table Output enter image description here

Current Minimum Working Example to Produce Table Above

\documentclass{article}

\usepackage[letterpaper,margin=1in]{geometry} % Handles geometry of page layout
\usepackage[table]{xcolor} % Colors tables nicely
\usepackage{tabularx}

\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686}
% Defines command \rowstyle for specifying style for entire row
\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand {\rowstyle}[1]{\gdef\currentrowstyle{#1} %
    #1\ignorespaces
}

\begin{document}

{\rowcolors{2}{black!5}{black!15} % Specify alternating row colors
    \begin{table}[h]
        \label{table_1} \centering
        \begin{tabularx}{\linewidth}{ +c^c^l^c }
            \hline
            \rowcolor{greyblue} \rowstyle{\bfseries} % Specify the header row is grey-blue in color and bold
            Header 1 & Header 2 & Really Long Header 3   & Header 4 \\ % Header row
            A    & B  & CDEFGHIJKLMNOPQRSTUVWXY  & Z    \\
            1    & 2  & 345  & 6    \\
            \hline
        \end{tabularx}
    \end{table}
}

\end{document}
1

Like this?

enter image description here

MWE:

\documentclass{article}
\usepackage[letterpaper,margin=1in]{geometry} % Handles geometry of page layout
\usepackage[table]{xcolor} % Colors tables nicely
\usepackage{tabularx}

\definecolor{greyblue}{rgb}{0.6353,0.6863,0.7686}
% Defines command \rowstyle for specifying style for entire row
\newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcolumntype{C}{>{\centering\arraybackslash}X}    % <-- new
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcommand {\rowstyle}[1]{\gdef\currentrowstyle{#1} %
    #1\ignorespaces
}

\begin{document}

    \begin{table}[htb]
\renewcommand\arraystretch{1.2}
\rowcolors{2}{black!5}{black!15} % Specify alternating row colors
\centering
\caption{}% label has sence only if it is present
    \label{table_1}
\begin{tabularx}{\linewidth}{+>{\hsize=0.15\hsize}C ^   % <-- changed
                              >{\hsize=0.15\hsize}C ^   % <-- changed
                              >{\hsize=0.55\hsize}L     % <-- changed
                              >{\hsize=0.15\hsize}C }   % <-- changed
    \hline
\rowcolor{greyblue}\rowstyle{\bfseries} % Specify the header row is grey-blue in color and bold
Header 1    & Header 2  & Really Long Header 3      & Header 4  \\ % Header row
A           & B         & CDEFGHIJKLMNOPQRSTUVWXY   & Z         \\
1           & 2         & 345                       & 6         \\
    \hline
\end{tabularx}
    \end{table}
\end{document}
  • Yes, that is what I'm looking for! – zephyr Nov 28 '16 at 19:46
  • I'm glad that answer help you. Just remark to third column. If cels in this columns will have text in more lines, the proposed L column will produce ragged text. With ragged2e package and instead raggedright in column L definition you will use, \RaggedRight, the text will be also hyphenated. – Zarko Nov 28 '16 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.