I’m trying to use an expel
floating point variable (\l_@@_last_machine_row_x_fp
) inside a calculation of a dimension (\l_@@_bar_width_dim
).
In the following code \BarA
works (with a solution from this answer), while \BarB
doesn’t work.
\documentclass[parskip]{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn\makeatletter
% @@ = mymodule
\dim_new:N \l_@@_bar_width_dim
\dim_new:N \l_@@_x_dim
\dim_set:Nn \l_@@_x_dim { 8mm }
\fp_new:N \l_@@_last_machine_row_x_fp
\NewDocumentCommand { \BarA } { m } {
\fp_add:Nn \l_@@_last_machine_row_x_fp { 1.5 }% arbitrary value
\dim_set:Nn \l_@@_bar_width_dim {
\fp_to_dim:n {
(#1 - \l_@@_last_machine_row_x_fp)
*
\l_@@_x_dim
}
}
\rule { \l_@@_bar_width_dim } { 1ex }
}
\NewDocumentCommand { \BarB } { m } {
\fp_add:Nn \l_@@_last_machine_row_x_fp { 1.5 }% arbitrary value
\dim_set:Nn \l_@@_bar_width_dim {
(#1 - \l_@@_last_machine_row_x_fp)
*
\l_@@_x_dim
}
\rule { \l_@@_bar_width_dim } { 1ex }
}
\ExplSyntaxOff\makeatother
\begin{document}
\BarA{5}% arbitrary value
\BarB{15}% arbitrary value
\end{document}
And I wonder why I need \fp_to_dim:n
in this case, since \l_@@_x_dim
is already a dimension multiplied by a unit free value (#1 - \l_@@_last_machine_row_x_fp)
.
My example code is taken from a package im developing at the moment and obviously does’t make much sense for itself ;-)
\dim_set:Nn \l_@@_bar_width_dim { \fp_to_decimal:n { ... } \l_@@_x_dim }
should also work (and would IMHO be a bit clearer). Anyway, you need to convert the fp from its internal representation into something usable in\dimexpr...\relax
(which in the end is what evaluates the dimension expression) – cgnieder Feb 4 '16 at 11:56\fp_to_decimal
is indeed clearer, but then I must omit the*
. Would you make an answer out of your comment? And you said that the fp has to be converted, then why(#1 - \fp_use:N \l_@@_last_machine_row_x_fp)
doesn’t work? – Tobi Feb 4 '16 at 13:20\xx_use:N \l_tmpa_xx
and when a single ` \l_tmpa_xx` is enough? – Tobi Feb 4 '16 at 13:21tl
is an exception in that the accessor is not required. – Joseph Wright♦ Feb 4 '16 at 13:27