0

I want to set the parameter \xx to define the tabular below.

However, I do not know how to expand the term (1-\xx) so the code below does not compile. I would appreciate your help.

\documentclass[12pt,letterpaper]{article}
\def\xx{0.3}
\begin{document}
\begin{tabular}{p{\xx\linewidth}p{(1-\xx)\linewidth}}
a&b\\
c&d
\end{tabular}
\end{document}
Chilote
  • 189
  • See https://tex.stackexchange.com/questions/245635/formal-syntax-rules-of-dimexpr-numexpr-glueexpr and look up tabularx – likethevegetable Jan 03 '22 at 03:04
  • thanks @likethevegetable but the line \begin{tabular}{p{\xx\linewidth}p{\the\dimexpr(1-\xx)\linewidth\relax}} produces the error message Illegal unit of measure (pt inserted). – Chilote Jan 03 '22 at 03:12
  • This isn't the answer you're exactly looking for, but in situations like this, the tabularx package is great. Include it, and then specify the total tabular width and use an X column to fill the table to width like so: \begin{tabularx}{\linewidth}{ p{\xx\linewidth} X } – likethevegetable Jan 03 '22 at 03:18
  • Well, that is the problem with minimal examples. I need something that works when we have more than 2 columns. Thanks, anyways. – Chilote Jan 03 '22 at 03:20
  • 1
    You can specify multiple X columns and their proportions. (ie one is half as wide as the other, but total fills up linewidth)..read the manual it's a very helpful package designed to help with problems like this. – likethevegetable Jan 03 '22 at 03:24
  • I want to create many tables in a document where all the columns will use certain proportions depending on some parameters like \xx. If I want to change the values of all the tables in the document, I do not need to make the changes manually on every column of every table. I will be able to just assign values to a few parameters. That is what I need. That can be done with tabular or tabularx, it does not really matter to me which one I use. The important point is how to expand expressions like (1-\xx) inside the options of tabular or tabularx. – Chilote Jan 03 '22 at 03:43

1 Answers1

4

You can use the xfp package's \fpeval command.

\documentclass[12pt,letterpaper]{article}
\usepackage{xfp}
\def\xx{0.3}
\begin{document}
\begin{tabular}{p{\xx\linewidth}p{\fpeval{1-\xx}\linewidth}}
a&b\\
c&d
\end{tabular}
\end{document}

But I'd go for a solution that uses tabularx

\documentclass[12pt,letterpaper]{article}
\usepackage{tabularx}
\def\xx{0.3}
\begin{document}
\begin{tabularx}{\linewidth}{ p{\xx\linewidth} X }
a&b\\
c&d
\end{tabularx}
\end{document}

If you're going to use these columns many times throughout your doc (like you mentioned in the comments), I would recommmend defining a column with the array package, eg. \newcolumntype{P}{\xx\linewidth}