6

Problem

I keep getting:

You can't use `\XeTeXcountglyphs` in vertical mode.

Attempt

\documentclass{article}
\usepackage{fontspec}
\usepackage{pgf}
\usepackage{pgffor}
\begin{document}
\pgfmathsetmacro\maxstep{\XeTeXcountglyphs\font}
\foreach \charstep in {1,...,\maxstep}{%
    \XeTeXglyph\charstep
}%
\end{document}

Note that I have since changed \pgfmathsetmacro to \pgfmathtruncatemacro so that I can type the following in my document and it makes sense (otherwise you get something like 2625.01; fractions of glyphs do not make sense):

 Number of glyphs: \maxstep
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036

2 Answers2

7

You can use \edef, however the indexing is 0 based so you need to reduce by 1 or you get an error while accessing the last slot.

\documentclass{article}
\usepackage{fontspec}
\usepackage{pgf}
\usepackage{pgffor}
\begin{document}

\edef\maxstep{\the\numexpr\XeTeXcountglyphs\font-1\relax}

\typeout{maxstep is \maxstep}

\foreach \charstep in {0,...,\maxstep}{%
    \XeTeXglyph\charstep
}%
\end{document}

the above is error free, although I don't show the image, the result is a little overfull

Overfull \hbox (4178.74983pt too wide) in paragraph at lines 13--14
David Carlisle
  • 757,742
  • I got rid of the Overfull \hboxwith by changing the line \XeTeXglyph\charstep to \XeTeXglyph\charstep{}\space – Jonathan Komar Mar 29 '15 at 10:24
  • @macmadness86 yes sure, I assumed you knew what final layout you wanted, a space or \\ or whatever, so I just left it as in the original. – David Carlisle Mar 29 '15 at 10:27
  • Ah ok. I did feel like I was "preaching to the choir" when I wrote that. You clearly would be one to know how to remedy that error! Also, I enjoyed the humor (a "little" overfull—just 4000+ points too wide:) – Jonathan Komar Mar 29 '15 at 10:29
4

\pgfmathsetmacro wants to see an explicit number, not an internal register. So \the\XeTeXcountglyphs\font would do, but it doesn't for another reason: trying to print the glyph corresponding to the highest count results in an error.

Solution: step back by 1.

\documentclass{article}
\usepackage{fontspec}
\usepackage{pgf}
\usepackage{pgffor}
\begin{document}

\pgfmathsetmacro\maxstep{\the\XeTeXcountglyphs\font-1}
\foreach \charstep in {1,...,\maxstep}{%
    \XeTeXglyph\charstep\space\space
}

\end{document}

The first \space is swallowed by \XeTeXglyph, the second one survives and spaces the result.

enter image description here

egreg
  • 1,121,712