15

Tried the printlen package, but it seems to not understand what 2.5in means:

\documentclass{minimal} 
\usepackage{printlen} 
\uselengthunit{in} 
\begin{document} 
\newlength{\advertwidth} 
\setlength{\advertwidth}{2.5in} 
\printlength{\advertwidth} 
\end{document} 

Typesetting the above yields 2.50049 in --- is there something better?

NB - in addition to the above, the project in question needs the packages:

\usepackage{calc,geometry,xcolor,graphicx,csvsimple}

the last in particular, conflicts w/ the answers initially provided.

0

4 Answers 4

10
\documentclass[preview,border=12pt,varwidth]{standalone}
\usepackage[nomessages]{fp}
\usepackage{printlen}
\makeatletter
\newcommand{\printer}[3][in]{%
    \FPeval\temp{round(\expandafter\strip@pt\csname#2\endcsname/72.27:#3)}%
    \temp#1}
\makeatother

\newlength\xxx
\setlength{\xxx}{2.5in}    
\parindent=0pt


\begin{document}    

\printer{xxx}{1}

\printer{xxx}{2}

\printer{xxx}{7}

\end{document}

enter image description here

16

The recently uploaded lengthconvert package seems to be what you're looking for:

\documentclass{article}
\usepackage{lengthconvert}
\Convertsetup{unit=in}
\begin{document}

\newlength{\advertwidth}
\setlength{\advertwidth}{2.5in}

\Convert{\advertwidth}

\Convert{72.27pt}

\end{document}

enter image description here

6
  • Unfortunately, it clashes with csvsimple --- I've emailed the two package authors, so hopefully this will get worked out.
    – WillAdams
    Jul 9, 2013 at 16:26
  • 4
    The macro \TrimSpaces in csvsimple should have an @ name, since it's an internal of the package. The clash is with xparse, rather than lengthconvert
    – egreg
    Jul 9, 2013 at 16:30
  • Prof. Sturm, the author of csvsimple pointed out in a private e-mail that re-ordering the packages would've been sufficient to fix this, but has indicated that he will change the relevant macro name in his next update. Thanks!
    – WillAdams
    Jul 10, 2013 at 16:31
  • 1
    @WillAdams No, it wouldn't be sufficient; loading csvsimple after xparse would avoid raising the error in this particular case, but would break xparse.
    – egreg
    Jul 10, 2013 at 16:33
  • 1
    Unfortunately, the lengthconvert package has not been updated to reflect some changes in expl3, so it should not be used, unless the fix proposed in tex.stackexchange.com/a/270885/4427 is applied.
    – egreg
    Mar 27, 2019 at 11:49
11

The obligatory expl3 solution:

\documentclass{minimal} 
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand { \printlengthas } { m m }
  { \dim_to_decimal_in_unit:nn {#1} { 1 #2 } #2 }
\ExplSyntaxOff 
\begin{document} 
\newlength{\advertwidth} 
\setlength{\advertwidth}{2.5in} 
\printlengthas{\advertwidth}{in}
\end{document} 

(\dim_to_unit:nn is currently experimental, but the idea is bound to stay: just a question of the name.)

Note: this currently requires a recent release of expl3: \dim_to_decimal_in_unit:nn was added to the CTAN version on 2014-07-15.

7
  • I see egreg's solution also uses expl3 internally: mine is a wrapper around basic TeX arithmetic only (lenghtconvert uses the L3 FPU).
    – Joseph Wright
    Jul 9, 2013 at 15:22
  • Unfortunately, this doesn't play well w/ csvsimple either.
    – WillAdams
    Jul 9, 2013 at 16:28
  • @WillAdams I think that should be asked as a separate question ('How to use csvsimple and xparse: clash over \TrimSpaces'). A solution is I think easy, as csvsimple uses \TrimSpaces only internally and only in one place, and this can be fixed using an alternative version from expl3. (This is separate from the question posed here, so two separate but linked questions would be better than one with two entirely separate parts.)
    – Joseph Wright
    Jul 9, 2013 at 16:38
  • dim_to_unit isn't documented :-( Jul 9, 2013 at 17:05
  • 1
    @WillAdams The fixed version of csvsimple has been uploaded on CTAN (and included in TeX Live)
    – egreg
    Sep 27, 2013 at 11:09
1

The function \printlen[Unit][FracNr]{length} formats the output of the unit in sans serif, which I don't like, so I use the following four macros to output a length in pt, in, cm and mm. The macros require the packages fp for non-integer division and siunitx.

\newcommand{\ScaledPointsInPoint}{\number \dimexpr 1pt \relax}

\newcommand*{\FPSizePt}[2][0]{%
\FPdiv\res{\number\dimexpr #2\relax}{\ScaledPointsInPoint}%
\FPeval\res{round(\res:#1)}%
\SI\res{pt}}

\newcommand*{\FPSizeIn}[2][2]{%
\FPdiv\res{\number\dimexpr #2\relax}{\ScaledPointsInPoint}%
\FPdiv\res{\res}{72.27}%
\FPeval\res{round(\res:#1)}%
\SI\res{in}%
}

\newcommand*{\FPSizeCm}[2][2]{%
\FPdiv\res{\number\dimexpr #2\relax}{\ScaledPointsInPoint}%
\FPdiv\res{\res}{72.27}%
\FPmul\res{2.54}{\res}%
\FPeval\res{round(\res:#1)}%
\SI\res{\cm}}

\newcommand*{\FPSizeMm}[2][2]{%
\FPdiv\res{\number\dimexpr #2\relax}{\ScaledPointsInPoint}%
\FPdiv\res{\res}{72.27}%
\FPmul\res{25.4}{\res}%
\FPeval\res{round(\res:#1)}%
\SI\res{\mm}}

Usage:

\newlength{\testlen}\setlength{\testlen}{5440 pt}
\FPSizeIn{\testlen}, \FPSizeCm[4]{\testlen}, \FPSizeMm{\testlen}, \FPSizePt{\testlen}.

Output in locale=DE: 75,27 in, 191,1941 cm, 1911,94 mm, 5440 pt.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .