# Set specific width for table colums relative to textwidth

Currently my code looks like this:

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}

\begin{document}

\begin{minipage}{\linewidth}
\subsubsection*{Finish Private Chat Success}
\centering
\begin{tabular}{|p{0.3\linewidth}|p{0.3\linewidth}|p{0.1\linewidth}|p{0.1\linewidth}|p{0.2\linewidth}|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & Something & Date & Word Word\\
\hline
\end{tabular}
\end{minipage}

\end{document}


My problem is, that the table does not fit the width of the page, I supposed the linewidth to be the space between left and right defined by geometry. But this does not seem to be the case, I already tried to do this with the tablex package but that was not successful either. This solution with minipage is what seems to fit my needs most. I would like to know how I have to calculate the width of each column to actually fit the width of the text area.

• Your table is still wider than the textwidth because there are white spaces on the left and right of each table column as well as the widths of the vertical lines, that are not taken into account by your approach. – leandriis Jun 11 '18 at 14:34
• Use something like p{\dimexpr 0.3\linewidth-2\tabcolsep}. – Ulrike Fischer Jun 11 '18 at 14:35
• How important is it that the column widths be in the ratio 3:3:1:1:2? – Mico Jun 11 '18 at 15:32

Rather than calculate the permissible column widths by hand, I would load the tabularx package and let it figure out the widths. Assuming you want the relative (usable) column widths to be in the ratio 3:3:1:1:2, you can achieve this goal along the lines shown in the code below.

In addition, I would also instruct LaTeX to typeset the material in the narrow columns ragged-right rather than fully justified.

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[vmargin=1.5cm,hmargin=2cm]{geometry}
\usepackage{tabularx,ragged2e}
\newcolumntype{L}[1]{>{\RaggedRight\arraybackslash%
\hspace{0pt}%  % to allow hyphenation of first word in a cell
\hsize=#1\hsize}X}
\setlength\extrarowheight{2pt} % for a less-cramped "look"
\begin{document}

\subsubsection*{Finish Private Chat Success}

\noindent
%% 1.5+1.5+0.5+0.5+1.0=5 = number of columns of type "X"
\begin{tabularx}{\textwidth}{| *{2}{L{1.5}|} *{2}{L{0.5}|} L{1.0}|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here
& And some long text here that is supposed to have automatic line breaks in this cell
& & Date & Word Word\\
\hline
Some text here
& And some long text here that is supposed to have automatic line breaks in this cell
& Something & Date & Word Word\\
\hline
\end{tabularx}
\end{document}

• Thank you for your help, this iss the solution I needed, as tabularx did not scale all my table entries the right way. Your macro did the trick. – user9670036 Jun 12 '18 at 11:46

The code in the question does not take into account the horizontal white space at the left and right of the text in every column as well as the width of the vertical lines. Therefor the width of the whole table adds up to more than the textwidth.

In order to correctly calculate the width to make the table exactly as wide as the textwidth you can make use of the calc package and \tabcolsep (width of the white space) and \arrayrulewidth (width of vertical line) as shown in the following MWE:

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{calc}
\usepackage{array}
\begin{document}
\noindent
\begin{tabular}{|p{0.3\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.3\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.1\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.1\textwidth-2\tabcolsep-1.2\arrayrulewidth}|p{0.2\textwidth-2\tabcolsep-1.2\arrayrulewidth}|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & text & Date & Word Word\\
\hline
\end{tabular}
\end{document}


If you prefer a more automated approach, the tabularx package might be useful. You could use its flexible width X column as shown in the following MWE:

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|l|X|l|l|l|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & text & Date & Word Word\\
\hline
\end{tabularx}
\end{document}


If you insist on the exact ratios that you used in your code, you might also be interesten in the tabu package, that offers an easy to use syntax as can be seen in the following MWE:

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,top=1.5cm,bottom=1.5cm,left=2cm,right=2cm]{geometry}
\usepackage{tabu}
\begin{document}
\noindent
\begin{tabu}to \textwidth{|X[3]|X[3]|X|X|X[2]|}
\hline
First & Second & Third & Fourth & Fifth\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & text & Date & Word Word\\
\hline
\end{tabu}
\end{document}


With tabularx and some improvements for the rules:

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, vmargin=1.5cm, hmargin=2cm, showframe]{geometry}
\usepackage{tabularx, makecell}

\begin{document}

\subsubsection*{Finish Private Chat Success}

{\centering\setcellgapes{4pt}\makegapedcells
\begin{tabularx}{\linewidth}{!{\vrule width 1pt}X|X|l|l|p{0.2\linewidth}!{\vrule width 1pt}}
\Xhline{1pt}
First & Second & Third & Fourth & Fifth\\
\Xhline{1pt}
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & & Date & Word Word\\
\hline
Some text here & And some long text here that is supposed to have automatic line breaks in this cell & Something & Date & Word Word\\
\Xhline{1pt}
\end{tabularx}
}

\end{document}