3

The output of the following code is affected whether calc package is included or not:

\documentclass{article}

\usepackage{tabularx}
% \usepackage{calc}
\newsavebox{\testbox}
\newlength{\testlen}

\newcommand{\boxx}[1]{%
 \savebox{\testbox}{#1}%
 \ifdim\wd\testbox>\testlen \global\setlength{\testlen}{\wd\testbox}\fi
 \parbox{\testlen}{#1}}%

\begin{document}
\begin{tabularx}{10cm}{|c|X|c|}
col 1 & col 2 & col 3 \\
\boxx{\mbox{}\hfill aaaaaaa\hfill\mbox{}} & xxxxxxxxxxxxx \par testlen value: \the\testlen & \boxx{bbbbbbbbbb} \\
\boxx{\mbox{}\hfill 50\hfill\mbox{}} & yyyyyyyyyyyy & \boxx{A\hfill1} \\
 90 & zzzzzzzzz & B\hfill2
\end{tabularx}
\end{document}

I'm taking advantage of the three pass of tabularx to determine which cell has the widest text to set a global length with this value. On the second pass all cells will have a parbox using this length.

Without calc, everything works as expected. With calc the length is not globalized and has no effect opening parboxes on other cells.

What to do? I need calc.

The application of all this is 1. I'm solving this situation.

  • Why do you need package calc? – user2478 Mar 3 '16 at 17:43
  • @Herbert because is a class that uses pbox package, which relies on calc package. I'm the author of that class (factura). Also I do some artithmetic on lengths based on calc, but the most important is the pbox package. – Emilio Lazo Mar 3 '16 at 17:55
  • with etex which is nowadays part of every tex engine it is easier to use \dimexpr and \numexpr – user2478 Mar 3 '16 at 18:25
3

I used the macros from calc. However, I would use \dimexpr without package calc

\documentclass{article}    
\usepackage{tabularx}
 \usepackage{calc}
\newlength\testA
\newlength\testB

\newcommand\boxx[1]{%
    \settowidth{\testA}{#1}%
    \ifdim\testA>\testB \global\testB=\testA\fi
    \parbox{\testB}{#1}}%

\begin{document}
    \begin{tabularx}{10cm}{|c|X|c|}
        col 1 & col 2 & col 3 \\
        \boxx{\mbox{}\hfill aaaaaaa\hfill\mbox{}} & xxxxxxxxxxxxx \par testlen value: \the\testB & \boxx{bbbbbbbbbb} \\
        \boxx{\mbox{}\hfill 50\hfill\mbox{}} & yyyyyyyyyyyy & \boxx{A\hfill1} \\
        90 & zzzzzzzzz & B\hfill2
    \end{tabularx}
\end{document}
  • Of course!! Pretty easy. Thank you @Herbert. – Emilio Lazo Mar 3 '16 at 19:54
1
\global\setlength{\testlen}{\wd\testbox}

has never been supported syntax.

Without the calc package it just happens to work by accident as \setlength just expands to \testlength=\wd\testbox\relax so the construct is \global\testlength=\wd\testbox\relax

With \calc package, \setlength is a complicated macro that has to parse the infix calc syntax and does so making may intermediate definitions. S depending on just how lucky or unlucky you are the \global could end up not prefixing an assignment at all, so make an error, or end up prefixing some internal temporary assignment. It will not make the assignment to \testlength global in either case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.