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
Best Answer
The argument of the
p
column type states the usable horizontal space, not the total horizontal space taken up by the column. Since the secondtabular
environment in your code contains 8 columns and 9 vertical-bar symbols, its total width is given by1\textwidth+16\tabcolsep+9\arrayrulewidth
. With default values of6pt
and0.4pt
for\tabcolsep
and\arrayrulewidth
, respectively, the excess width over\textwidth
is99.6pt
-- roughly 1.4 inches, or ca 3.5cm. Yikes!You wrote,
Then, by all means, use either a
tabularx
or atabular*
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 theX
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 atabular*
environment, with 8 instances of LaTeX's basicc
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 thesiunitx
package to typeset scientific units.