2

Well fit tables in LaTeX is hard for me, and I don't know if there is a manual with good practices about it. I need to create a table with S columns contending numbers, but the header of those columns are very long and I can't find a way to fit it into the page with the tricks learned here.

My MWE could be something like:

\documentclass[fontsize=11pt,paper=letter,headings=small,bibliography=totoc,DIV=9,headsepline=true]{scrartcl}
\usepackage[utf8]{inputenx}
\usepackage[TS1,T1]{fontenc}
\usepackage{textcomp}
\usepackage[spanish,mexico]{babel}
\usepackage[intlimits]{amsmath}
\usepackage{xspace}
\usepackage{xkeyval}
\usepackage{array,multirow,tabularx,ragged2e,booktabs}
%\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
%\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\usepackage{rotating}
\usepackage{colortbl}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{
    output-decimal-marker = {,}, 
    range-units = brackets,
    list-final-separator = { \translate{and} },
    list-pair-separator = { \translate{and} },
    range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a }%substitute the right word here
\ExplSyntaxOff


\begin{document}

\begin{table}[htb]
    \caption{Capacidad de generación eléctrica en México}
    \label{tab:CGE}
    \centering

    \begin{tabularx}{\textwidth}{@{} l S S[table-format=6.2] S[table-format=3.2]@{}}
    \toprule
        & \textbf{Permisos de la CRE} & \textbf{Capacidad de generación de energía} [\si{\giga\watt\hour}] & \textbf{Porcentaje}\\
    \midrule
    % Pública
    \textbf{Energía de generación propia} & & \textbf{169,900.00} & \textbf{42.25} \\
    \midrule
    \textbf{Pemex} & &  & \\
    \textbf{Autoabastecimiento} & 31 & 2526.40 & 0.61 \\
    \textbf{Cogeneración} & 18 & 7049.04 & 1.75 \\ 
    \textbf{Subtotal} & & 9575.44 & 2.38 \\
    % Privada
    \midrule
    \textbf{Autoabastecimiento} & 486 & 67145.80 & 16.70 \\
    \textbf{Cogeneración} & 109 & 18153.66 & 4.51 \\
    \textbf{Producción independiente} & 30 & 105634.80 & 26.27 \\
    \textbf{Exportación} & 8 & 13295.60 & 3.31 \\
    \textbf{Importación} & 44 & 2061.30 & 0.51 \\
    \textbf{Pequeña producción} & 178 & 12212.60 & 3.04 \\
    \textbf{Usos Propios Continuos} & 36 & 1523.50 & 0.38 \\
    \textbf{Pemex-Cogeneración} & 1 & 2628.00 & 0.65 \\
    \textbf{Subtotal} & 844 & 222655.26 & 55.37 \\
    \textbf{Total CRE} & 892 & 232230.70 & 57.75 \\
    \midrule
    \textbf{Total} & 892 & 402130.14 & 100 \\
    \bottomrule
    \end{tabularx}
\end{table}

\end{document}

As you can see my first issue is to fit the table inside the page. The second one is put in boldface the units in the title row. A third issue is to add a column in the left side of the table to specify the public and private generation, bit I think that could be easy to solve after the first one.

Sorry if this is a duplicate question, but I didn't found an answer in the questions I was reading before to ask.

Mico
  • 506,678
Aradnix
  • 3,735
  • 2
  • 20
  • 43

4 Answers4

4

Try to use smaller font (if necessary) and put columns header in more lines, something like this: enter image description here

For above table I added package makecell and put columns header into theader from makecell:

\documentclass[fontsize=11pt,paper=letter,headings=small,bibliography=totoc,DIV=9,headsepline=true]{scrartcl}
    \usepackage[utf8]{inputenx}
    \usepackage[TS1,T1]{fontenc}
    \usepackage{textcomp}
    \usepackage[spanish,mexico]{babel}
    \usepackage[intlimits]{amsmath}
    \usepackage{xspace}
    \usepackage{xkeyval}
    \usepackage{array,multirow,tabularx,ragged2e,booktabs}
    \usepackage{makecell}% added
\renewcommand\theadfont{\bfseries}% added
%\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
%\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
    \usepackage{rotating}
    \usepackage{colortbl}
    \usepackage[per-mode=symbol]{siunitx}
\sisetup{
    output-decimal-marker = {,},
    range-units = brackets,
    list-final-separator = { \translate{and} },
    list-pair-separator = { \translate{and} },
    range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a }%substitute the right word here
\ExplSyntaxOff

    \usepackage[active,floats,tightpage]{preview}
\setlength\PreviewBorder{1em}

    \begin{document}
\begin{table}[htb]
    \small% added
    \caption{Capacidad de generación eléctrica en México}
    \label{tab:CGE}
    \centering

    \begin{tabularx}{\textwidth}{>{\bfseries}l S S[table-format=6.2] S[table-format=3.2]}
    \toprule
        &   {\thead{Permisos\\ 
             de la CRE}}
            &   {\thead{Capacidad de\\ generación de \\ 
                       energía [\si{\giga\watt\hour}]}}
                &   {\thead{Porcentaje}}\\
    \midrule
    % Pública
Energía de generación propia
                    & & \textbf{169,900.00} & \textbf{42.25} \\
    \midrule
Pemex               & &  & \\
Autoabastecimiento  & 31 & 2526.40 & 0.61 \\
Cogeneración        & 18 & 7049.04 & 1.75 \\
Subtotal            & & 9575.44 & 2.38 \\
    % Privada
    \midrule
Autoabastecimiento  & 486 & 67145.80 & 16.70 \\
Cogeneración        & 109 & 18153.66 & 4.51 \\
Producción independiente  & 30 & 105634.80 & 26.27 \\
Exportación         & 8   & 13295.60 & 3.31 \\
Importación         & 44  & 2061.30 & 0.51 \\
Pequeña producción  & 178 & 12212.60 & 3.04 \\
Usos Propios Continuos    & 36 & 1523.50 & 0.38 \\
Pemex-Cogeneración  & 1   & 2628.00 & 0.65 \\
Subtotal            & 844 & 222655.26 & 55.37 \\
Total CRE           & 92  & 232230.70 & 57.75 \\
    \midrule
Total               & 892 & 402130.14 & 100 \\
    \bottomrule
    \end{tabularx}
\end{table}
    \end{document}

For final form you can play with broken column's header into appropriate number of lines and select other font size.

Edit As @Manuel say in his comment, in the first column can be omited \textbf{....} if you in column type you include >{\bfseries}l. I now considering this and correct the code above.

Zarko
  • 296,517
  • 2
    You can put >{\bfseries} l in the declaration of columns, and avoid using \textbf{..} in each cell of the first column. – Manuel Jun 06 '15 at 08:29
3

The solution is the makecell package, which allows for line breaks in cells, and a common formatting of column heads. Added a small vertical padding in rows and replaced the l column with X. Finally I took the liberty to correct some inconsistencies in the formatting of boldface numbers in the second row.

\documentclass[fontsize=11pt,paper=letter,headings=small,bibliography=totoc,DIV=9,headsepline=true]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[TS1,T1]{fontenc}
\usepackage{textcomp}
\usepackage[spanish,mexico]{babel}
\usepackage[intlimits]{amsmath}
\usepackage{xspace}
\usepackage{xkeyval}
\usepackage{array,multirow,tabularx,ragged2e, booktabs}
%\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
%\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\usepackage{rotating}
\usepackage{colortbl}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{
output-decimal-marker = {,},
range-units = brackets,
list-final-separator = { \translate{and} },
list-pair-separator = { \translate{and} },
range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a }%substitute the right word here
\ExplSyntaxOff

\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadalign{bc}
\setcellgapes{1.5pt}

\begin{document}

\begin{table}[htb]
  \centering\makegapedcells
  \caption{Capacidad de generación eléctrica en México}
  \label{tab:CGE}
  \begin{tabularx}{\linewidth}{@{}>{\bfseries\RaggedRight}X S S[table-format=6.2] S[table-format=2.2]@{}}
    \addlinespace
    \toprule
    \addlinespace[-0.3ex]
    & {\thead{Permisos\\ de la CRE}} & {\thead{Capacidad de \\generación de\\ energía (\si{\giga\watt\hour})}}
    & {\thead{Porcentaje}}\\[-0.6ex]
    \midrule
    % Pública
    Energía de generación propia & & \textbf{169\,900,00} & \textbf{42,25} \\
    \midrule
    Pemex & & & \\
    Autoabastecimiento & 31 & 2526.40 & 0.61 \\
    Cogeneración & 18 & 7049.04 & 1.75 \\
    Subtotal & & 9575.44 & 2.38 \\
    % Privada
    \midrule
    Autoabastecimiento & 486 & 67145.80 & 16.70 \\
    Cogeneración & 109 & 18153.66 & 4.51 \\
    Producción independiente & 30 & 105634.80 & 26.27 \\
    Exportación & 8 & 13295.60 & 3.31 \\
    Importación & 44 & 2061.30 & 0.51 \\
    Pequeña producción & 178 & 12212.60 & 3.04 \\
    Usos Propios Continuos & 36 & 1523.50 & 0.38 \\
    Pemex-Cogeneración & 1 & 2628.00 & 0.65 \\
    Subtotal & 844 & 222655.26 & 55.37 \\
    Total CRE & 892 & 232230.70 & 57.75 \\
    \midrule
    Total & 892 & 402130.14 & 100 \\
    \bottomrule
  \end{tabularx}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350
  • Thakns for the suggestions and the solution. I want to add a narrow column with text rotated 90° in the left side similar to this one but without colouring the table. How to specify it before the {\bfseries\RaggedRight}X? – Aradnix Jun 07 '15 at 06:45
  • @Aradnix: Do you mean some multirow with rotated text along the whole height of the table? Could you explain with more details? – Bernard Jun 07 '15 at 10:00
  • Yes, in fact I want to use multirow with 90° CCW rotated text for specify the public and private energy production in that table. I was thinking to add that row in each subtotal row. But I'm not sure how to specify that column. With l or X I Have an error. – Aradnix Jun 08 '15 at 07:42
3

You don't have to change the font size or abandon the basic tabularx structure. All you have to do, really, is to (a) change the column type for the header cells of the first two numeric columns to (a centered version of) the X column type, and (b) place curly braces around the right-hand most column header, viz. \textbf{Porcentaje}, to tell LaTeX to center its contents.

To unify and simplify the look of the table, I would further suggest you use the siunitx option group-minimum-digits = 4, that you not bold-face the numbers in first row of the table, and to indent a few entries in the first column (and omit a couple of horizontal lines). Finally, if you specify the type of the first column as >{\bfseries}l rather than just as l, you needn't type \textbf in all subsequent cells of that column.

enter image description here

\documentclass[fontsize=11pt,paper=letter,headings=small,
     bibliography=totoc,DIV=9,headsepline=true]{scrartcl}
\usepackage[utf8]{inputenx}
\usepackage[TS1,T1]{fontenc}
\usepackage{textcomp}
\usepackage[spanish,mexico]{babel}
\usepackage[intlimits]{amsmath}
\usepackage{xspace}
\usepackage{xkeyval}
\usepackage{array,multirow,tabularx,ragged2e,booktabs}
\newcolumntype{C}{>{\Centering\arraybackslash\hspace{0pt}}X}
\usepackage{rotating}
\usepackage{colortbl}
\usepackage[per-mode=symbol]{siunitx}
\sisetup{
    output-decimal-marker = {,},
    group-minimum-digits = 4, 
    range-units = brackets,
    list-final-separator = { \translate{and} },
    list-pair-separator = { \translate{and} },
    range-phrase = { \translate{to (numerical range)} },
}
\ExplSyntaxOn
\providetranslation [ to = Spanish ]
{ to~(numerical~range) } { a }%substitute the right word here
\ExplSyntaxOff


\begin{document}

\begin{table}[htb]
    \caption{Capacidad de generación eléctrica en México}
    \label{tab:CGE}
    %%\centering  % not needed

    \begin{tabularx}{\textwidth}{@{} >{\bfseries}l 
         S[table-format=3.0]
         S[table-format=6.2] 
         S[table-format=3.2] @{}}
    \toprule
    & \multicolumn{1}{C}{\textbf{Permisos de la CRE}} 
    & \multicolumn{1}{C}{\textbf{Capacidad de generación de energía} [\si{\giga\watt\hour}]} & 
    {\textbf{Porcentaje}}\\
    \midrule
    % Pública
    Energía de generación propia & & 169900.00 &  42.25 \\
    \midrule
    Pemex & &  & \\
    Autoabastecimiento & 31 & 2526.40 & 0.61 \\
    Cogeneración & 18 & 7049.04 & 1.75 \\ 
    \addlinespace
    \quad Subtotal & & 9575.44 & 2.38 \\
    % Privada
    \addlinespace
    Autoabastecimiento & 486 & 67145.80 & 16.70 \\
    Cogeneración & 109 & 18153.66 & 4.51 \\
    Producción independiente & 30 & 105634.80 & 26.27 \\
    Exportación & 8 & 13295.60 & 3.31 \\
    Importación & 44 & 2061.30 & 0.51 \\
    Pequeña producción & 178 & 12212.60 & 3.04 \\
    Usos Propios Continuos & 36 & 1523.50 & 0.38 \\
    Pemex-Cogeneración & 1 & 2628.00 & 0.65 \\
    \addlinespace
    \quad Subtotal & 844 & 222655.26 & 55.37 \\
    \addlinespace
    \quad Total CRE & 892 & 232230.70 & 57.75 \\
    \midrule
    Total & 892 & 402130.14 & 100.00 \\
    \bottomrule
    \end{tabularx}
\end{table}

\end{document}
Mico
  • 506,678
  • Thank you. Interesting solution, I started with this one for avoid add more packages: Unfortunately the problem with alignment remains. Good trick that of the boldface to the head of the table. I had already seen your recommendation to better align the digits with group- minimum- digits = 4 . However I have a question: If you see the units at the header of the table, they don't appear in boldface. I think there is a way to change that, but I don't remember what siunitx option do it. – Aradnix Jun 06 '15 at 19:25
  • 1
    @Aradnix - I must state up front that my personal style is not to use boldface unless it's absolutely required. (In my own working papers, I sometimes don't even set the section headers in bold!) From my subjective point of view, then, you're already massively overusing boldface in the table. If this were my own table, I wouldn't use any bold-facing at all; for sure, I certainly wouldn't show the units in bold. If it were absolutely necessary to provide some specific emphasis, I might italicize the two "Subtotal" strings and the "Total CRE" string. – Mico Jun 06 '15 at 19:35
  • I see, I also avoid to use boldface, but this time is not my decision, is something the boss wants. But I'm agree with you, let me change it and present a new layout with less boldface usage. Thanks again. – Aradnix Jun 06 '15 at 19:45
1

There is one simple technique using the default booktabs package.

\begin{tabular}[c]{@{}l@{}} table \\ heading \end{tabular}

For example, In your table, the heading 'Permisos de la CRE' can be broken in two lines as:

\begin{tabular}[c]{@{}l@{}} Permisos \\ de la CRE \end{tabular}

This can be done in any rows or columns in a table, not just in heading.

Werner
  • 603,163
Arish
  • 11