[Tex/LaTex] Lualatex: Font table with examples

fontsfontspecluatex

I wanted to create a table of the fonts on my machine with short examples of each. So I got the function below and tried to modify to print the font in its typeface. (LuaLatex)

\documentclass[10pt,a4paper]{article}
\usepackage{fontspec}
\usepackage{luacode,luaotfload,luatextra}
\usepackage[margin=18mm]{geometry}

\begin{document}
\begin{luacode}
myfonts=dofile(fonts.names.path.localdir..'/otfl-names.lua')
--tex.print('\\begin{verbatim}\\par')
for i,v in ipairs(myfonts.mappings) do
  --tex.print('\\fontspec{' .. v.fontname .. '}')   % most recent attempt
  --tex.print('\\setmainfont{' .. v.fontname .. '}') % first attempt
tex.print(v.familyname..', '..v.fontname..'\\par')
end
--tex.print('\\end{verbatim}\\par')
\end{luacode}

\end{document}

Both of the commented lines does not work (fontspec or setmainfont).

How should go about doing this with lua?

Best Answer

This improves Caramdir's answer to make the table easier to read:

\documentclass{article}

\usepackage{fontspec}
\setmainfont{Latin Modern Mono Light}

\usepackage{luacode}

\usepackage[margin=18mm]{geometry}
\parindent=0pt

\usepackage{longtable,makecell}
\renewcommand\arraystretch{2}

\begin{document}
\begin{luacode}
myfonts=dofile(fonts.names.path.localdir..'/otfl-names.lua') -- TeX Live 2012 or earlier
-- myfonts=dofile(fonts.names.path.path) -- TeX Live 2013
teststring = "Sphinx of black quartz, judge my vow."

tex.print("\\begin{longtable}{ll}\\hline")

for i,v in ipairs(myfonts.mappings) do
  -- Stop early for testing purposes.
  if i > 20 then break end

  tex.print('\\makecell[l]{\\bfseries')
  tex.print(-2, v.familyname)
  tex.print('\\\\[-1ex] \\scriptsize')
  tex.print(-2, v.fontname)
  tex.print('} & \\LARGE\\fontspec{' .. v.fontname .. '}')
  tex.print(-2, teststring)
  tex.print('\\\\ \\hline')
end
tex.print("\\end{longtable}")
\end{luacode}

\end{document}

enter image description here

Related Question