# Math operations on lengths

I have a table with three columns and I want the table to be distributed on \textwidth of the page.

I tried:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\textwidth/3}{| c | c | c |} %Math operation here.
\hline
looooooong & long & very loooong \\
long & very looooooong & long\\
\hline
\end{tabularx}
\end{document}

• use begin{tabularx}{\textwidth}{| X | c | X |} for example. Or three times the column specifier X if you have three wide colums with linebreaks – user2478 Dec 28 '17 at 18:41
• While you're using tabularx, it seems like you haven't read the tabularx documentation. Please do! – Werner Dec 28 '17 at 18:44
• @Herbert wow it worked, what does X do? I have read this page en.wikibooks.org/wiki/LaTeX/Tables#The_tabularx_package – Shayan Dec 28 '17 at 18:46
• @Werner just saw your comment thanks, I read that The X column speciﬁcation will be converted to p{hsome valuei} once the correct column width has been calculated. – Shayan Dec 28 '17 at 18:50
• But It interests me to know, can you do math operations on such things in LaTeX? Can you save \textwidth in a variable and then use it later on? Or can I somehow print \textwidth at least to I know the size and then divide it by 3 myself? – Shayan Dec 28 '17 at 18:52

\documentclass{article}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{| X | X | X |} \hline
\lipsum[5] & \lipsum[5] & \lipsum[5]\\\hline
\end{tabularx}

\noindent
\begin{tabularx}{\textwidth}{
| >{\hsize=0.5\hsize}X
| X
| >{\hsize=1.5\hsize}X |} \hline
\lipsum[5] & \lipsum[5] & \lipsum[5]\\\hline
\end{tabularx}

\end{document}


If you need the possibility of a pagebreak then use xltabular instead.

• This is what I wanted to distribute text evenly and lipsum generates an article? nice, Do I need the first tabularx in my code as well or was it just to generate the article? EDIT: Nevermind :D – Shayan Dec 28 '17 at 19:41
• No, this doesn't distribute evenly (Share the extra space with other columns) – Shayan Dec 28 '17 at 19:49
• The first tabularx has three columns with equal width and the second tabularx shows how you can get X columns with different width. The lipsum package is only for generating dummy text. – user2478 Dec 28 '17 at 20:04

The following two approaches provides the same output; the first uses calculations on \textwidth to distribute the columns evenly, while the second uses tabularx's X-columns to compute the lengths:

\documentclass{article}

\usepackage{tabularx}

\begin{document}

\setlength{\parindent}{0pt}% Just for this example

\begin{tabular}
{ | *{3}{p{\dimexpr(\textwidth - \arrayrulewidth)/3 - 2\tabcolsep - \arrayrulewidth} |} }
\hline
looooooong & long & very loooong \\
long & very looooooong & long \\
\hline
\end{tabular}

\begin{tabularx}{\textwidth}{ | *{3}{X |} }
\hline
looooooong & long & very loooong \\
long & very looooooong & long \\
\hline
\end{tabularx}

\end{document}


X-columns default to p{<len>} after <len> is determined. If you want to change this, you can use

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


to vertically centre the column entries, for example (m{<len>} is provided by array, which is loaded by tabularx).

• I have \usepackage{array} but \begin tabular \begin{tabularx}{\textwidth}{| X m{4.7cm} | X m{4.7cm} | X m{4.7cm} |} doesn't seem to work. – Shayan Dec 28 '17 at 19:12
• (That's how I would go about centering columns vertically with tabular – Shayan Dec 28 '17 at 19:15
• @Shayan: For centering columns vertically, use \renewcommand{\tabularxcolumn}[1]{m{#1}} and then \begin{tabularx}{\textwidth}{ | X | X | X | }. – Werner Dec 28 '17 at 19:18
• oh ok, I didn't know what that command was about, I don't know [1]{m{#1} does, also can't yet upvote comments so thanks – Shayan Dec 28 '17 at 19:22
• If you see the output of your code which you inluded on your answer, there's blank space on each column, any way to get rid of the blank space to make the table smaller? – Shayan Dec 28 '17 at 20:08