6

I've seen \dimexpr used in the context of setting the width of the columns of a two-column table:

\newlength\q
\setlength\q{\dimexpr .5\textwidth -2\tabcolsep}
\noindent\begin{tabular}{|p{\q}|p{\q}|}
...

However, I am not sure what \dimexpr .5\textwidth -2\tabcolsep means here because I don't know what \dimexpr means. I've found the question formal syntax rules of \dimexpr \numexpr \glueexpr, but it doesn't explain what \dimexpr is.

mhdadk
  • 181
  • 2
    \dimexpr evaluates an expression involving dimensions. – Henri Menke May 17 '21 at 11:17
  • 2
    It enables you to make arithmetic operations on dimensions. This was introduced by eTeX. – Bernard May 17 '21 at 11:18
  • So \dimexpr .5\textwidth -2\tabcolsep evaluates to 0.5 * \textwidth - 2 * \tabcolsep? – mhdadk May 17 '21 at 11:21
  • @Bernard Sounds like an answer – Joseph Wright May 17 '21 at 11:32
  • @HenriMenke Sounds like an answer – Joseph Wright May 17 '21 at 11:32
  • 1
    @mhdadk: Yes, that's correct. – Bernard May 17 '21 at 11:37
  • 1
    Small note: One should put a \relax after the expression one wants to evaluate using \dimexpr (and friends) to stop parsing. Most of the time this doesn't hurt, but could result in unwanted behaviour if one forgets the \relax. – Skillmon May 17 '21 at 11:46
  • Thanks a lot for the help! – mhdadk May 17 '21 at 11:48
  • 1
    And if you need the evaluated dimension in text rather than as an internally stored length, use \the\dimexpr.... Here, \the causes the internal length that follows to be typeset as a string. This is useful sometimes when you need to store the dimension for later use or visual inspection. p.s. \dimexpr stands for "dimensional expression". – Steven B. Segletes May 17 '21 at 11:56
  • 1
    To follow up, 3\dimexpr2pt+1pt\relax would equal the internal representation of 9pt, whereas 3\the\dimexpr2pt+1pt\relax would equal the string 33pt (i.e., a 3 concatenated with 3pt). – Steven B. Segletes May 17 '21 at 12:06
  • 1
    The OLD way of doing it was \q=0.5\textwidth \advance\q by -2\tabcolsep. – John Kormylo May 17 '21 at 13:18
  • @John is it no longer possible to do this? – mhdadk May 17 '21 at 13:23
  • 1
    You can still do it. It's sort of like writing in assembly language (one operation per line of code). The only thing is that you can only \multiply or \divide dimens with counts. – John Kormylo May 17 '21 at 13:30
  • Anyone know why I can't use \dimexpr like this: \dimexpr{...}? I get a "missing number, treated as zero." error. However, I get no errors if I write \dimexpr(...). – mhdadk May 17 '21 at 13:50
  • 1
    @mhdadk: because that's not the syntax of how \dimexpr should be used (you linked to a question at the end of your post; this is explained in that post). If you want to visually delimit the arguments to \dimexpr, you should follow Skillmon's suggestion and write \dimexpr .... \relax. – Willie Wong May 17 '21 at 14:01

0 Answers0