4

I am trying to have table exactly between margins using \textwidth. Unfortunately, when I am using \textwidth my table is too large and I do not know why. (my code is for django template, but I was checking it in TexStudio also).

I am trying to have such tables (note that 18cm is my \textwidth, as I have a4paper and both left and right margins = 1,5cm)

    \begin{tabular}{|p{18cm}|}
    \hline
    aaa\\
    \hline
    \end{tabular}


\begin{tabular}{|p{.08\textwidth}|p{.08\textwidth}|p{.08\textwidth}|p{.08\textwidth}|p{.16\textwidth}|p{.2\textwidth}|p{.2\textwidth}|p{.12\textwidth}|}
    
\hline
    PWM & O2 & CO  & CO2 & Temp. spalin & Ciśni. gaz przed EV & Ciśn gazu ze EV & Prąd jon. \\ \hline
    -   & \% & ppm & \%  & oC           & mbar                & mbar            & microA    \\ \hline
    &    &     &     &              &                     &                 &           \\ \hline
\end{tabular}

where all widths sum up to 1, but table is too wide (the same result was when I specified width in cm). In preamble I am using for whole document, but it doesn't matter if it is commented out or not :

\setlength{\parindent}{0cm}

All of the preamble :

\documentclass[8pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[left=1.50cm, right=1.50cm, top=1.50cm, bottom=1.50cm, width=18cm]{geometry}
\usepackage{showframe}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{multirow}
\usepackage{titlesec}
\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}

\titleformat{\section}
{\normalfont\Large\bfseries\filcenter}{\thesection}
{0em}{}
% \usepackage{roboto}
% \usepackage[sfdefault, thin]{roboto}

\setlength{\parindent}{0cm}
\newcommand{\mySmallSkip}{\hspace*{10mm}}
\pagestyle{fancy}
%\usepackage{showframe} 
\fancyhf{}
\renewcommand{\footrulewidth}{0.4pt}% default is 0pt
\textheight=160mm

and my table is like : enter image description here

6
  • 4
    When calculating the column widths you did not take into account the \tabcolsep and the width of the vertical lines. This is why your table is wider than the textwidth.
    – leandriis
    Jun 15, 2020 at 18:36
  • Do you need automatic linebreaks inside the columns? If not, wouldn't simple l type columns be sufficient? Your table would then be narrower than the textwidth.
    – leandriis
    Jun 15, 2020 at 18:39
  • 1
    8pt is not a valid option for the article document class.
    – Mico
    Jun 15, 2020 at 18:42
  • @leandriis - thank you ! i want to have control over width of each column, so l is not an option - or I am wrong ?
    – Jakub
    Jun 15, 2020 at 19:25
  • @Mico thanks for pointing me that out
    – Jakub
    Jun 15, 2020 at 19:29

2 Answers 2

4

The argument of the p column type states the usable horizontal space, not the total horizontal space taken up by the column. Since the second tabular environment in your code contains 8 columns and 9 vertical-bar symbols, its total width is given by 1\textwidth+16\tabcolsep+9\arrayrulewidth. With default values of 6pt and 0.4pt for \tabcolsep and \arrayrulewidth, respectively, the excess width over \textwidth is 99.6pt -- roughly 1.4 inches, or ca 3.5cm. Yikes!

You wrote,

I am trying to have table exactly between margins using \textwidth.

Then, by all means, use either a tabularx or a tabular* environment and set its width to \textwidth.

The first table in the following screenshot uses a tabularx column type and uses a centered version of the X column type for all 8 columns. It calculates the relative column widths as follows. First, notice that the 8 columns' widths must satisfy the ratio 2:2:2:2:4:5:5:3. Second, the sum of the relative column widths must be equal to 8, the number of columns of type X. Thus, 25x=8, or x=8/25=32/100. Thus, the relative column widths work out to 0.64 (4 times), 1.28, 1.6 (2 times), and 0.96.

While the first table uses \hline directives and lots of vertical bars (9, in fact!), the second table shows that a vast aesthetic improvement is easily achieved simply by getting rid of all vertical bars and using well-spaced alternatives to \hline.

Since automatic line-breaking doesn't actually seem to be needed, really, using tabularx is a form of overkill. The final table shows how to obtain nearly the same output with a tabular* environment, with 8 instances of LaTeX's basic c column type. Isn't *{8}{c} a lot easier than *{4}{C{0.64}} C{1.28} *{2}{C{1.6}} C{0.96}?

Whatever else you do, do employ a package such as \mhchem to typeset the names of chemical compounds and the siunitx package to typeset scientific units.

enter image description here

\documentclass[a4paper]{article} % A4: 21cm wide
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[margin=1.5cm]{geometry}
\usepackage{tabularx,mhchem,siunitx,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hsize=#1\hsize}X}

\begin{document}
\setlength\tabcolsep{4pt} % for the first two tables

\noindent
\begin{tabularx}{\textwidth}{| *{4}{C{0.64}|} C{1.28}| *{2}{C{1.6}|} C{0.96} |}
\hline
PWM & \ce{O2} & \ce{CO} & \ce{CO2} & Temp.\ spalin & Ciśni.\ gaz przed EV & Ciśn gazu ze EV & Prąd jon. \\
\hline
-- & \% & ppm & \%  & \si{\celsius} & \si{\milli\bar} & \si{\milli\bar} & \si{\micro\ampere} \\
\hline
\end{tabularx}

\vspace{1cm}
\noindent
\begin{tabularx}{\textwidth}{@{} *{4}{C{0.64}} C{1.28} *{2}{C{1.6}} C{0.96} @{}}
\toprule
PWM & \ce{O2} & \ce{CO} & \ce{CO2} & Temp.\ spalin & Ciśni.\ gaz przed EV & Ciśn gazu ze EV & Prąd jon. \\
\midrule
-- & \% & ppm & \%  & \si{\celsius} & \si{\milli\bar} & \si{\milli\bar} & \si{\micro\ampere} \\
\bottomrule
\end{tabularx}

\vspace{1cm}
\setlength\tabcolsep{0pt}
\noindent
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} *{8}{c} }
\toprule
PWM & \ce{O2} & \ce{CO} & \ce{CO2} & Temp.\ spalin & Ciśni.\ gaz przed EV & Ciśn gazu ze EV & Prąd jon. \\
\midrule
-- & \% & ppm & \%  & \si{\celsius} & \si{\milli\bar} & \si{\milli\bar} & \si{\micro\ampere} \\
\bottomrule
\end{tabular*}

\end{document}
3

Here are two versions of your table. In both, I have used the siunitx package for the units as well as the chemformula package for the chemical formulae. In the first table, I have corrected the column width calculation to also take into account \tabcolsep and \arrayrulewidth. In the second table, I have used simple l type columns since the table in narrow enough to fit into the textwidth even without having to use p type columns:

enter image description here

\documentclass[a4paper]{article} % removed invalid 8pt option
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[margin=1.50cm]{geometry} % summarized left=1.50cm, right=1.50cm, top=1.50cm, bottom=1.50cm to margin=1.50cm
\usepackage{showframe}
\usepackage{siunitx} % used for \si{...} command
\usepackage{chemformula} % added for \ch{...} command
\usepackage{calc}

\setlength{\parindent}{0cm}

\begin{document}


\begin{tabular}{|*{4}{p{.08\textwidth-2\tabcolsep-\arrayrulewidth}|}
                p{.16\textwidth-2\tabcolsep-\arrayrulewidth}|
                p{.2\textwidth-2\tabcolsep-\arrayrulewidth}|
                p{.2\textwidth-2\tabcolsep-\arrayrulewidth}|
                p{.12\textwidth-2\tabcolsep-2\arrayrulewidth}|}
    
\hline
    PWM & \ch{O2}       & CO       & \ch{CO2} & Temp. spalin   & Ciśni. gaz przed EV  & Ciśn gazu ze EV      & Prąd jon.           \\ \hline
    -   & \si{\percent} & \si{ppm} & \%       & \si{\celsius}  & \si{mbar}            & \si{mbar}            & \si{\micro\ampere}  \\ \hline
        &               &          &          &                &                      &                      &                     \\ \hline
\end{tabular}

\begin{tabular}{|l|l|l|l|l|l|l|l|}
    
\hline
    PWM & \ch{O2}       & CO       & \ch{CO2} & Temp. spalin   & Ciśni. gaz przed EV  & Ciśn gazu ze EV      & Prąd jon.           \\ \hline
    -   & \si{\percent} & \si{ppm} & \%       & \si{\celsius}  & \si{mbar}            & \si{mbar}            & \si{\micro\ampere}  \\ \hline
        &               &          &          &                &                      &                      &                     \\ \hline
\end{tabular}

\end{document}
1
  • many thanks ! I still do not have proper units etc as it is first draft of document and I am working on layout.
    – Jakub
    Jun 15, 2020 at 19:29

You must log in to answer this question.

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