# \settowidth in preamble wrong with fontspec+lualatex w/ MWE

I am getting some strange behaviour using luaLaTeX and a rather simple command definition. I am using TeXLive 2012, Linux x86_64.

When \settowidth is used in the preamble to compute the width of some characters in the following example, the width is terribly underestimated, and when that width is used to negatively indent some text, it does not line up.

If the \settowidth operating is moved inside the document environment, the calculation is just fine.

Here is a minimum working example:

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{fontspec}

\newdimen\arrowwidth
\settowidth{\arrowwidth}{$\rightarrow$\ }
\newcommand{\note}[1]{\noindent\hskip-\arrowwidth{$\rightarrow$\ }{#1}}

\begin{document}
\lipsum[1]
%\settowidth{\arrowwidth}{$\rightarrow$\ }
\note{HELLO WORLD!}

\lipsum[2-2]

\end{document}


If you uncomment the line above the \note{HELLO WORLD}, everything works as expected.

Changing the 12pt option also changes the issue somewhat drastically.

The → character isn't the problem. You can substitute roman text for the right arrow and it will still give problems.

Does anyone have any idea what causes this? Can anyone else reproduce it?

-
Welcome to TeX.sx! Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. – Kurt Nov 1 '12 at 15:34
Before using \settowidth in the preamble set issue the command \rmfamily. This forces your font choices to take effect at that point rather than being delayed to the \begin{document} command. – Andrew Swann Nov 1 '12 at 15:50

For technical reasons, fontspec delays some font settings after the preamble has been completely read in. In particular, the font size is still 10pt when you do the \settowidth command. You can solve the issue by forcing \normalsize in the command:

\settowidth{\arrowwidth}{\normalsize$\rightarrow$\ }


But there's a slicker way to define your \note command:

\newcommand{\note}[1]{\noindent\makebox[0pt][r]{$\rightarrow$\ }#1}


that doesn't require any measuring.

-
Thanks! That zero-width box trick is quite brilliant. I could have sworn I tried the \normalsize, but it didn't work. Perhaps I'm mistaken. – Timtro Nov 1 '12 at 15:48
I'm wondering if this isn't a bug. fontspec doesn't really delay the size setting but changes it: before loading fontspec \f@size is 12pt and afterwards it is 10pt. – Ulrike Fischer Nov 1 '12 at 16:06
@Timtro Ulrike (she's a Mrs, by the way) is right. I remember that this has been discussed somewhere. – egreg Nov 1 '12 at 16:26