2

I want to calculate the ratio of two lengths in Latex, so that I can then stretch an en dash by that ratio. For example, in the code below, I'd like to replace the "0.7" value of FakeStretch with the ratio of newdashw/dashw.

How can I do this?

\documentclass{article}
\usepackage{fontspec}
\setmainfont{TeX Gyre Schola}  % the font might change from doc to doc

\def\hairspace{\kern .08333em} % make a very thin space

\newlength\dashw
\settowidth\dashw{\normalsize--} % width of en dash in this font

\newlength\newdashw
\setlength\newdashw\dashw
\addtolength\newdashw{-0.16667em} % subtract 2*hairspace

% make a figure dash: want to replace "0.7" with "newdashw / dashw"
\newcommand\figdash{%
    \hairspace{\addfontfeature{FakeStretch=0.7}--}\hairspace}

\begin{document}
Jenny's number: 867\figdash 5309
\end{document}
4
  • 2
  • Thanks, Paul. That page does help, both the pgf and DivideLengths solutions work for me. I would like to actually understand it, however. I get scaling up the numerator and performing integer division. I don't get the conversion of the result to a "float". Philipp, the DivideLengths author, said you have to "have TeX treat the result as a dimension, convert that to the result string, and strip the unit suffix". How does DivideLengths do that? I hate this feeling of knowing less today than I (thought I) did yesterday. – dedded Sep 11 '14 at 3:05
  • Do you use XeLaTeX or LuaLaTeX? – Mico Sep 11 '14 at 7:13
  • I'm using xelatex, but perhaps one could also stretch or shrink a dash with pdflatex and microtype. I haven't played with that package. – dedded Sep 11 '14 at 11:31
2

You can use the expandable macros provided by package xintfrac

lengthratio

\documentclass{article}
\usepackage{fontspec}
%\setmainfont{TeX Gyre Schola}  % the font might change from doc to doc

% on my installation, the fonts in TL tree must be made known via file name to
% xetex 
\setmainfont[ExternalLocation]{texgyreschola-regular}

\def\hairspace{\kern .08333em } % make a very thin space

\newlength\dashw
\settowidth\dashw{\normalsize--} % width of en dash in this font

\newlength\newdashw
\setlength\newdashw\dashw
\addtolength\newdashw{-0.16667em} % subtract 2*hairspace

\usepackage{xintfrac}% macros for expandable computations 

% make a figure dash: 
\newcommand\figdash{%
    \hairspace
    {\addfontfeature{FakeStretch=\xintRound {4}{\newdashw/\dashw}}--}%
    \hairspace
}

% make a big figure dash: 
\newcommand\figbigdash{%
    \hairspace
    {\addfontfeature{FakeStretch=\xintRound {4}{3*\newdashw/\dashw}}--}%
    \hairspace
}

\begin{document}

FakeStretch=\xintRound {4}{\newdashw/\dashw}

Jenny's number: 867\figdash 5309

FakeStretch=\xintRound {4}{3*\newdashw/\dashw}

Jenny's number: 867\figbigdash 5309

\end{document}
3
  • Thank you much. New folks can't upvote, else I'd upvote your answer. I'm nearly certain that this is the solution I will use. And if Paul hadn't pointed me to the \DivideLengths post, I'd "accept" this answer (and still might eventually). But now I'm seeking entlightment on \DivideLengths's operation. I'll have more time for study tomorrow night or over the weekend. – dedded Sep 11 '14 at 12:44
  • @dedded don't worry about upvotes. Remark: if the \figdash command is to be used many many times, then it is more efficient to do once in the preamble \edef\FakeStretchValue{\xintRound{4}{\newdashw/dashw}} and to define there \figdash via FakeStretch=\FakeStrechValue. – user4686 Sep 11 '14 at 14:09
  • @dedded Are you sure you can't up vote? You can certainly accept jfbu's answer by licking on the tick. – user30471 Sep 11 '14 at 14:20
2

Another solution, using my package calculator: command \LENGTHDIVIDE divides two lengths and returns a number. For example,

\LENGTHDIVIDE{\newdashw}{\dashw}{\theRatio}

\documentclass{article}

   \usepackage{calculator} % Load calculator

\usepackage{fontspec}
\setmainfont{TeX Gyre Schola}  
\def\hairspace{\kern .08333em} 
\newlength\dashw
\settowidth\dashw{\normalsize--} 
\newlength\newdashw
\setlength\newdashw\dashw
\addtolength\newdashw{-0.16667em} 

    \LENGTHDIVIDE{\newdashw}{\dashw}{\theRatio} % \theRatio=\newdashw/\dashw

\newcommand\figdash{%
    \hairspace{\addfontfeature{FakeStretch=\theRatio}--}\hairspace} % Using \theRatio
\begin{document}
Jenny's number: 867\figdash 5309
\end{document}

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.