# How do I make a table with cells with a width equal to a fraction of the width of the page (excluding margins)?

This is what I tried:

\documentclass[a4paper]{article}

% Margins
\usepackage[margin=1in]{geometry}

% Prevent some weird error
\usepackage[english,ngerman]{babel}

% Prevent paragraph indentation
\usepackage{parskip}
\setlength{\parindent}{0pt}

\usepackage{tabularx} % for the m thing

\begin{document}

\begin{tabular}{ m{0.2\linewidth} m{0.8\linewidth} }
& \begin{flushright} Right aligned text \end{flushright} \\
\end{tabular}

\begin{flushleft}
Left aligned text
\end{flushleft}

\end{document}


(I want the text in the second column to be right-aligned).

So, this works, but the only problem is to do with the margins. If you look at the left margin in the left aligned text, you can see that it is bigger than the margin for the right aligned text in the table.

I can't figure out the problem here. I actually found this forum post about the topic. It seems to be pretty much what I wanted. However, the \arraycolsep thing doesn't work at all in my environment, and I tried converting it into a long table but ran into errors.

How else can I fix this?

• Simplest fix: the tabular environment gives some space between the columns, 0.2+a tiny bit+0.8 gives more than 1. Reduce the width of the second column to something like 0.75 (or adjust both). The ninja'ed answer by @AboAmmar is the proper way to do it. – Huang_d May 21 '17 at 12:39

There is a small space tabcolsep equal to 6pt at either side of the cell text, you must compensate for this when you calculate the width of the column. In your case, you have 2 columns, which means 4\tabcolseps in total, or 4*6 = 24pt to be subtracted from the calculated length.

Also, instead of using the environments \begin{flushright} .. \end{flushright} or \begin{flushleft} .. \end{flushleft} you can use a new column of type R or L, respectively as defined below.

\documentclass[a4paper]{article}

% Margins
\usepackage[margin=1in]{geometry}
\usepackage{showframe}    % <===== just to see the margins, remove after testing

% Prevent some weird error
\usepackage[english,ngerman]{babel}

% Prevent paragraph indentation
\usepackage{parskip}
\setlength{\parindent}{0pt}

\usepackage{array} % for the 'm' column type

\begin{document}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}

\begin{tabular}{ |L{\dimexpr0.2\linewidth-12pt}| R{\dimexpr0.8\linewidth-12pt}|}
Left aligned text  & Right aligned text \\
\end{tabular}

\begin{flushleft}
Left aligned text
\end{flushleft}

\end{document}


• Oh, thank you! Also for the showframe -- it really helps in seeing what's going on. I was trying to subtract 12pt from 0.2\linewidth, so it didn't work. (I also couldn't figure out what the default space size was!) – Ryland Doe May 21 '17 at 12:57
• @AboAmmar: geometry already has a showframe option. – Bernard May 21 '17 at 13:06

The simplest way to achieve this is with tabularx, because this package computes the exact table width, taking into account the values of \tabcolsep. Here, the X column type was redefined to be a m column, and we can set the relative width of such columns with >{\hsize=xx \hsize}, the different xxs being subject to the condition that their sum is equal to the number of such columns (whence here, the values 0.4 and 1.6 in order to have a ratio 1:4. The coefficients are proportional to the barycentric coordinates, so to say). \documentclass[a4paper]{article}

% Margins
\usepackage[showframe, margin=1in]{geometry}

% Prevent some weird error
\usepackage[english,ngerman]{babel}

% Prevent paragraph indentation
\usepackage{parskip}

\usepackage{tabularx} % for the m thing

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}

\begin{tabularx}{\linewidth}{>{\hsize=0.4\hsize}X >{\hsize=1.6\hsize\raggedleft\arraybackslash}X}
& Right aligned text \\
Left aligned text. A short left-aligned text. & Some other right-aligned text. A rather long right-aligned text. A rather long right-aligned text.
\end{tabularx}

\end{document}