Based on this question, I know how to show regular lengths expressed in arbitrary units. However, I need to do the same for rubber lengths (showing the plus and minus) and the printlen
package doesn't support that except for pt. Is there an equivalent that will work for rubber lengths?
Add a comment
|
2 Answers
etex has additional primitives for extracting the components so that you can print them separately:
\documentclass{article}
\usepackage{printlen}
\begin{document}
\the\parskip
The \verb|\parskip| is \printlength{\dimexpr\parskip\relax} which is also
\uselengthunit{in}\printlength{\dimexpr\parskip\relax} and
\uselengthunit{mm}\printlength{\dimexpr\parskip\relax}.
The \verb|\gluestretch\parskip| is \printlength{\gluestretch\parskip} which is also
\uselengthunit{in}\printlength{\gluestretch\parskip} and
\uselengthunit{mm}\printlength{\gluestretch\parskip}.
The \verb|\glueshrink\parskip| is \printlength{\glueshrink\parskip} which is also
\uselengthunit{in}\printlength{\glueshrink\parskip} and
\uselengthunit{mm}\printlength{\glueshrink\parskip}.
\end{document}
-
Is this a good command:
\newcommand{\myprtlen}[2]{\uselengthunit{#1}\printlength{\dimexpr#2\relax}~plus~\printlength{\gluestretch#2}~minus~\printlength{\glueshrink#2}}
– SigurCommented Feb 20, 2015 at 19:20 -
1@Sigur you might want to also test
\gluestretchorder
and\glueshrinkorder
in case the plus or minus component is fill glue like\hfill
Commented Feb 20, 2015 at 19:23 -
Using @DavidCarlisle's answer I created the following function:
\newcommand{\printrubberlength}[1]{\printlength{#1}\ifdim\gluestretch#1=0pt\relax\else\ plus \printlength{\gluestretch#1}\fi\ifdim\glueshrink#1=0pt\relax\else\ minus \printlength{\glueshrink#1}\fi}
which nicely fits my bill. I don't have fills to worry about here. Commented Feb 20, 2015 at 19:36
Here's a version using expl3
that also takes care of infinite components.
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\printglue}{O{pt}m}
{ % #1 = unit, #2 = glue
\rpspringuel_print_glue:nn { #1 } { #2 }
}
\cs_new_protected:Npn \rpspringuel_print_glue:nn #1 #2
{
\skip_set:Nn \l_rpspringuel_input_skip { #2 }
\dim_set:Nn \l_rpspringuel_natural_dim { \l_rpspringuel_input_skip }
\dim_set:Nn \l_rpspringuel_stretch_dim
{ \rpspringuel_gluestretch:n { \l_rpspringuel_input_skip } }
\dim_set:Nn \l_rpspringuel_shrink_dim
{ \rpspringuel_glueshrink:n { \l_rpspringuel_input_skip } }
\int_set:Nn \l_rpspringuel_stretch_order_int
{ \rpspringuel_gluestretchorder:n { \l_rpspringuel_input_skip } }
\int_set:Nn \l_rpspringuel_shrink_order_int
{ \rpspringuel_glueshrinkorder:n { \l_rpspringuel_input_skip } }
% print the components
\dim_to_decimal_in_unit:nn { \l_rpspringuel_natural_dim } { 1#1 }
\,#1
\c_space_tl
plus
\c_space_tl
\int_case:nn { \l_rpspringuel_stretch_order_int }
{
{ 0 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_stretch_dim } { 1#1 }\,#1 }
{ 1 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_stretch_dim } { 1pt }\,fil }
{ 2 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_stretch_dim } { 1pt }\,fill }
{ 3 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_stretch_dim } { 1pt }\,filll }
}
\c_space_tl
minus
\c_space_tl
\int_case:nn { \l_rpspringuel_shrink_order_int }
{
{ 0 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_shrink_dim } { 1#1 }\,#1 }
{ 1 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_shrink_dim } { 1pt }\,fil }
{ 2 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_shrink_dim } { 1pt }\,fill }
{ 3 } { \dim_to_decimal_in_unit:nn { \l_rpspringuel_shrink_dim } { 1pt }\,filll }
}
}
\skip_new:N \l_rpspringuel_input_skip
\dim_new:N \l_rpspringuel_natural_dim
\dim_new:N \l_rpspringuel_stretch_dim
\dim_new:N \l_rpspringuel_shrink_dim
\int_new:N \l_rpspringuel_stretch_order_int
\int_new:N \l_rpspringuel_shrink_order_int
\cs_new:Npn \rpspringuel_gluestretch:n #1
{
\etex_gluestretch:D \etex_glueexpr:D #1\scan_stop:
}
\cs_new:Npn \rpspringuel_glueshrink:n #1
{
\etex_glueshrink:D \etex_glueexpr:D #1\scan_stop:
}
\cs_new:Npn \rpspringuel_gluestretchorder:n #1
{
\etex_gluestretchorder:D \etex_glueexpr:D #1\scan_stop:
}
\cs_new:Npn \rpspringuel_glueshrinkorder:n #1
{
\etex_glueshrinkorder:D \etex_glueexpr:D #1\scan_stop:
}
\ExplSyntaxOff
\begin{document}
\printglue{\parskip}
\printglue{\fill}
\newlength\mylength
\setlength\mylength{10cm plus 1cm minus 1filll}
\printglue[cm]{\mylength}
\printglue[mm]{10cm plus 1cm minus 1filll}
\end{document}
Note that there's no public interface for \gluestretch
, \gluestretchorder
, \glueshrink
and \glueshrinkorder
, so I defined one.
Rounding can give unexpected results; this might be improved using the fp
module.
-
Off topic: did you type the whole code or do you have auto completion editor?– SigurCommented Feb 21, 2015 at 1:03
-
@Sigur: chat.stackexchange.com/transcript/message/19587864#19587864 :-) Commented Feb 21, 2015 at 2:15
-
1@Sigur Where's the problem?
;-)
Some judicious “copy, paste and replace” makes repetitive code easy to type.– egregCommented Feb 21, 2015 at 9:29