10

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?

7

enter image description here

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}} – Sigur Feb 20 '15 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 – David Carlisle Feb 20 '15 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. – rpspringuel Feb 20 '15 at 19:36
6

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.

enter image description here

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.