Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am getting an error when I try to set a dimension register with a l3calc/calc type dimension expression using LaTeX3 \dim_set:Nn command. Even wrapping the whole expression into \dim_eval:n{} doesn't help. What can I do?

\documentclass{article}
\usepackage{expl3}
\usepackage{l3calc}

\begin{document}

\newlength{\mydim}

\setlength\mydim{15pt*\ratio{5pt}{10pt}} %this works
\typeout{\the\mydim} %7.5pt

\ExplSyntaxOn
\dim_set:Nn\mydim{15pt*\ratio{5pt}{10pt}} %error
\dim_set:Nn\mydim{\dim_eval:n{15pt*\ratio{5pt}{10pt}}} %same error
\ExplSyntaxOff

\typeout{\the\mydim} %don't get here

\end{document}
share|improve this question
    
For later readers. As of July 2012, LaTeX3 provides an expandable floating point unit, so all those computations can be done (albeit more slowly than in dimension expressions) with a natural syntax: \fp_set:Nn \myfp { 15pt * 5pt / (10pt) }, or to get a dimension, \dim_set:Nn \mydim { \fp_to_dim:n { 15pt * 5pt / (10pt) } }. The parenthesis may or may not be needed depending on the precise revision of our code. –  Bruno Le Floch Jun 25 '12 at 15:22

2 Answers 2

up vote 2 down vote accepted

dim_set uses e-TeX syntax, not calc syntax. Either use the functions from the l3calc module (calc_dim_set etc.), or convert your expression to e-TeX syntax:

\calc_dim_set:Nn\mydim{15pt*\ratio{5pt}{10pt}}
\dim_set:Nn\mydim{5pt*15/10}
share|improve this answer
1  
I think that a simple ratio function is probably desirable. Also note that we've taken l3calc out of the main expl3 code block as there are some issues with the implementation and with the overlap with e-TeX. –  Joseph Wright May 12 '11 at 10:41
    
Many thanks for your helpful answer! –  AlexG May 12 '11 at 10:48
    
@Joseph: Will l3calc become deprecated eventually? –  AlexG May 12 '11 at 10:51
    
@Alexander: It already is, in the sense that expl3 no longer loads it. (l3calc is an early try-out for the LaTeX3 approach, and as I said there are bits which no longer make sense.) –  Joseph Wright May 12 '11 at 10:57
    
Also note that expl3 deliberately loads calc to avoid issues if people are expecting l3calc to be loaded. –  Joseph Wright May 12 '11 at 11:28

The way that calc works is that it patches \setlength to do a lot of scanning in order to work. As a result, if you look at the definition of \ratio outside of \setlength it is just \relax. On the other hand, \dim_set:Nn is much more akin to directly setting a TeX dimen register: the definition is (in primitives)

\protected \def \dim_set:Nn #1#2 { #1 \dimexpr #2 \relax }

while \dim_eval:n is

\def \dim_eval:n #1 { \the \dimexpr #1 \relax }

So when you try to do

\dim_set:Nn \mydim { 20pt * \ratio { 5pt } { 10pt } }

\ratio is \relax, and you get an error as the dimension expression ends early.

Now, on what to do, I guess that this is going to need a little though from the LaTeX3 team. We've recently moved l3calc out of expl3 as some of it is no longer that useful, but we know that bits certainly are. What I guess we need here is an expandable \dim_ratio:nn function to return the ratio between two dimension expressions. I'll schedule that for addition to expl3: probably there won't be a CTAN release until after the TeX Live 2011 freeze, but I'll post the code as an edit here once it's done.


I now have some code for an expandable ratio function which can therefore be used inside \dim_set:Nn:

\cs_new_nopar:Npn \dim_ratio:nn #1#2
  { \dim_ratio_aux:n {#1} / \dim_ratio_aux:n {#2} }
\cs_new_nopar:Npn\dim_ratio_aux:n #1
  { \exp_after:wN \int_value:w \dim_eval:w #1 \dim_eval_end: }
\cs_new_protected_nopar:Npx \dim_ratio_aux:w
  {
    \cs_set_nopar:Npn \exp_not:N \dim_ratio_aux:w ##1 \tl_to_str:n { pt }
      {##1}
  }
\dim_ratio_aux:w

This will go into the development version of expl3 later today. (This works in sp, so the numbers will be quite large if given in pt but the code should be reliable.)

share|improve this answer
    
@Joseph: Thanks for your thorough explanation. –  AlexG May 12 '11 at 10:52
    
@Joseph: Great! But upload to CTAN will be deferred? –  AlexG May 12 '11 at 12:08
    
@Alexander: Unless it is urgent, then yes. We are currently doing some work to overhaul parts of expl3, and are wary of sending that to CTAN too close to the TeX Live freeze in case there are issues. However, if it's urgent I can arrange a release this month. –  Joseph Wright May 12 '11 at 12:44
    
@Joseph: This would be very helpful, if you don't mind. I am writing a package for CTAN and maybe I could submit it before the TeX Live freeze. Do you know when the freeze will be? –  AlexG May 12 '11 at 12:48
    
@Alexander: OK, I'll move the code over and arrange a release. –  Joseph Wright May 12 '11 at 12:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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