# using division to calculate column size

I am converting from another format and creating tables with varying numbers of columns. I would like the column to be approximately layed out. If there are two columns, I want each column to get half the space. Something like this:

\begin{longtable}{ p{\textwidth/2}  p{\textwidth/2} }


That is, for two columns, I want to divide the textwidth by two. Documentation elsewhere suggests that division should be possible using the forward slash character.

However, what I get is that both columns are the size of the full textwidth, and each cell starts with "/2". It is as if the /2 is being interpreted as "default text" to put in every cell, even the title row cells. See below:

Very strange. Every example I can find on the web has column width done by multiplying the textwidth by a decimal number. But that is inconvenient for me: When I have three columns I want it to be divided by three, and so on. I don't have the decimal for that fraction readily available.

Any idea what to do to get division working?

• Just a guess, but try with \dimexpr\textwidth/2. – Phelype Oleinik Oct 31 '18 at 22:55
• Why don't you use package ltablex or xltabular? – Bernard Oct 31 '18 at 23:16
• @Bernard - I have quite an investment in longtable having switched a couple years ago from tabular. It has specific features I need. To investigate ltablex or xltabular would require doing a detailed comparison to make sure it meets the other requirements. There are so many packages to use, but that is more of a problem than a solution. – AgilePro Nov 1 '18 at 15:06
• ltablexbrings the functionalities of longtable to tabularx (same syntax). As to the more recent xltabular it loads ltablex but does not redefine tabularx to avoid some of its drawbacks.. On the whole, if you know how to manage longable there should be no difficulties in using one of those packages. – Bernard Nov 1 '18 at 18:33

You can always in TeX (with no extra package) multiply a dimension register by a decimal number:

\documentclass{article}

\begin{document}

\begin{tabular}{ | p{.2\linewidth} | p{.25\linewidth} | }
a & b
\end{tabular}

\end{document}


If you want to use syntax with slash for division, wrap it in \dimexpr:

\documentclass{article}

\begin{document}

\begin{tabular}{ | p{\dimexpr\linewidth/5\relax} | p{\dimexpr\linewidth/4\relax} | }
a & b
\end{tabular}

\end{document}


Inside \dimexpr you can also do additions etc..., but the syntax 0.2*\linewidth is as illegal with it as it was with original Knuth TeX. You must not use the * at this location. The advantage of \dimexpr is to allow things such as

\dimexpr0.2\linewidth+3\tabcolsep-0.9876\dimexpr15pt\relax+\textwidth/25\relax


Notice that for some years now, the pdflatex format builds only on a pdftex which has the e-TeX extensions, inclusive of \dimexpr.

The calc package dates from an earlier era. You may still use it for some of its facilities such as \widthof, and chances are some package you use still loads it. It does not bring any computational facility that \dimexpr does not offer: none of them allow you to compute square roots for example (and computing a square root raises the question of the unit of your dimension).

The xfp solution is definitely overkill for adjusting by hand some column dimensions using multiplications and additions, but if you do need powers or square-root this is natural choice (there are other packages doing such computations).

• Do you have any idea why the "/2" then appears in every cell? Is that expected behavior? – AgilePro Nov 1 '18 at 15:03
• I did not check the full details but if you try \dimen0=\dimen4/2 (using TeX primitives) you will get that the dimension stored in TeX dimension register number 4 is copied in to dimension register number 0, and then /2 is part of the typeset stream. – user4686 Nov 4 '18 at 20:54

By default you can only use minimal calculation within length settings, specifically multiplication (like 0.5\textwidth or 0.3\linewidth). As such, using \textwidth/2 wouldn't work out-of-the-box. If you want to use that, then you'll need e-TeX's \dimexpr:

\dimexpr\textwidth/2\relax


The trailing \relax is optional, depending on the use-case.

The process is automated by the calc package which updates LaTeX com­mands \set­counter, \ad­dto­counter, \setlength and \ad­dtolength to take arithmetic arguments. Here's an example:

\documentclass{article}

\usepackage{calc}

\begin{document}

\begin{tabular}{ | p{\linewidth/5} | p{\linewidth/4} | }
a & b
\end{tabular}

\begin{tabular}{ | p{\linewidth/5+10pt} | p{\linewidth/4-10pt} | }
a & b
\end{tabular}

\end{document}


If you want to perform more complex arithmetic on lengths, I'd suggest using xfp. xfp's \fpeval converts lengths to pt and strips that dimension when performing calculations. So, one would typically use \fpeval{<length calc>}pt to specify a length. Here's another example:

\documentclass{article}

\usepackage{xfp}

\begin{document}

\begin{tabular}{ | p{\fpeval{\linewidth/5 - 4^2}pt} | p{\fpeval{sqrt(3^3) + \linewidth/4}pt} | }
a & b
\end{tabular}

\hspace*{\fpeval{\arrayrulewidth+\tabcolsep}pt}%
\rule{\fpeval{\linewidth/5 - 4^2}pt}{1pt}%
\hspace{\fpeval{2\tabcolsep+\arrayrulewidth}pt}%
\rule{\fpeval{sqrt(3^3) + \linewidth/4}pt}{1pt}

\end{document}