21

I am using various macros that I have fine-tuned with units like pt and cm. I use those macros normally in certain places (say, in main body text or in footnote text). To properly generalize those macros, I would like to replace the absolute units by relative ones.

Say I have a macro \newcommand*{\aalso}{\textsl{and also}\hspace{1.1pt}}. Say I normally use that macro in footnotes. But now I realize that I made a mistake and would like to change the length 1.1pt to something of the form ...em so that I can use the macro elsewhere. How do I get the right number? I assume that the right way is to define some macro like \emConverter{1.1pt} that, if issued in a footnote context, gives me the length of 1.1pt in a footnote context converted to a relative length in em. It doesn't need to be a LaTeX-parsable value; it's okay if this is just printed out directly into the document, for me to then manually write the new em-value into the source code.

Some relevant questions:

  • 1
    Since you are willing to do it manually you can just use the conversion table given in What are the various units (ex, in, pt, etc.) expressed in mm?. Once those are converted to em, ex they should be correct independent of if they are in the footnote. I don't think it makes sense to convert based on the actual footnote value as then they won't scale if the fonts are changed. – Peter Grill Feb 23 '13 at 07:09
  • See for example output of `\documentclass{article} \usepackage[paperheight=2cm]{geometry}

    \begin{document} In text \verb|\hspace{1.0em}| is x\hspace{1.0em}x. \footnote{In footnote this space is smaller: x\hspace*{1.0em}x.} \end{document}`.

    – Peter Grill Feb 23 '13 at 07:13
  • @PeterGrill The tables contain too many variables so that for someone not wanting to bother looking up font internals the tables are a risk. Because these values are context-dependent and I have fine-tuned them in some very specific contexts, I'd just like a macro that tells me the right value directly (under my assumption that my fine-tuned values will sensibly scale if I replace them by (say) an em-value that is exactly right in the original context where I fine-tuned the (say) pt-value). – Lover of Structure Feb 23 '13 at 15:21

2 Answers2

23
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\convertto}{mm}
    % #1 = em or ex (or any other unit)
    % #2 = dimen to convert
 {
  \texttt{#2~=~\fp_to_decimal:n { (#2)/(1#1) }#1}
 }
\ExplSyntaxOff

\begin{document}
\convertto{em}{1.1pt}

\convertto{ex}{1.1pt}

\Large
\convertto{em}{1.1pt}

\convertto{ex}{1.1pt}

\end{document}

enter image description here

A variant for also showing a given length (explicit or implicit) in a different unit of measure (default mm); I also added rounding to five digits

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\convertto}{mm}
    % #1 = em or ex (or any other unit)
    % #2 = dimen to convert
 {
  \texttt{#2~=~\fp_to_decimal:n { round ( (#2)/(1#1), 5 ) }#1}
 }
\DeclareExpandableDocumentCommand{\thelength}{ O{mm} m }
 {
  \fp_to_decimal:n { round ( #2/1#1, 5 ) } #1
 }
\ExplSyntaxOff

\begin{document}
\convertto{em}{1.1pt}

\convertto{ex}{1.1pt}

\Large
\convertto{em}{1.1pt}

\convertto{ex}{1.1pt}

\thelength{\textwidth}

\thelength[cm]{\textwidth}

\end{document}

enter image description here

An example application; we want to modify the output of layout so that it uses millimeters. We need only \thelength:

\documentclass{article}
\usepackage{xparse}
\usepackage{layout}


\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\thelength}{ O{mm} m }
 {
  \fp_to_decimal:n { round ( (#2)/(1#1), 5 ) } #1
 }
\ExplSyntaxOff

% redefine the output macro
\makeatletter
\renewcommand*{\lay@value}[2]{\thelength{\csname #2\endcsname}}
\makeatother

\begin{document}
\layout
\end{document}

I only show the relevant part of the output, with the lengths

enter image description here

egreg
  • 1,121,712
19

You can use pgfmath to compute the conversion:

\pgfmathsetmacro\len{1.1pt/1em}

Here is a complete example:

enter image description here

\documentclass{article}
\usepackage[paperwidth=12cm,paperheight=6cm,vmargin=1cm]{geometry}
\usepackage{pgfmath}
\pagestyle{empty}
\newcommand\pttoem[1]{%
  \pgfmathsetmacro\len{#1pt/1em}Here, with the current font, #1\,pt is
  \len{}\,em.
}
\begin{document}
\raggedright
\texttt{\pttoem{1.1}} \textbf{\pttoem{1.1}}
\pttoem{1.1}\footnote{\pttoem{1.1}}

{\large \pttoem{1.1}\par}
\end{document}
Paul Gaborit
  • 70,770
  • 10
  • 176
  • 283