Posts tagged ‘Tutorial’

Using individual glyphs from a OpenType Font in LuaLaTeX

I am currently preparing a tutorial on the Texas Instruments BAII Plus Professional Calculator in LaTeX. TI provides the font for the different keystrokes as TTF and PFB/PFM which makes it way easier to typeset the symbols.

The first step was to convert the font to OTF format. I tried TTF 2 OTF first without luck as the resulting OTF did not have any symbols in it. Using PFB 2 OTF worked better, the generated OTF had supposedly all the keys in it.

In the next step I wanted to generate an LaTeX overview of all symbols. Using the information from https://tex.stackexchange.com/questions/103704/how-to-properly-install-and-use-a-new-font-with-lualatex and https://tex.stackexchange.com/questions/25249/how-do-i-use-a-particular-font-for-a-small-section-of-text-in-my-document/37251 I came up with the following (don’t forget to install the OTF before!):

\documentclass[11pt]{article}
 
\usepackage[left=1cm,right=1cm,landscape,a4paper]{geometry}
\usepackage{fontspec}
 
\newfontfamily\tifont{BA2Plus Symbols}
 
\usepackage{luacode}
\usepackage{longtable,array,xcolor,listings}
\begin{luacode*} 
function print_glyphs(maxCols,maxChars) 
  local id = font.current()         -- geht Font ID
  local fnt = font.getfont(id)
  local col = 1
  local maxU4 = 15*(16^3+16^2+16+1)
  a = {}
  for k, v in pairs(fnt.characters) do
    a [#a + 1] = k
  end
  table.sort(a)
  for i, k in ipairs(a) do
    if i >= maxChars then break end
    if col == 1 then
      if k > maxU4 then
        tex.sprint(string.format("U+%06x", k))
      else
        tex.sprint(string.format("U+%04x", k))
      end
      tex.sprint("&") 
    end
    if (i) then
      tex.sprint(string.format([[\char%i]], k))
    else
     tex.sprint("~")
    end
    if col == maxCols then              -- Line finished?
      tex.sprint([[\\\cline{2-]] .. maxCols+1 .. "} ")  -- Yes
      col = 1                           -- newline
    else
      tex.sprint("&")                   -- no, Print &
      col = col + 1                     -- next column
    end
  end
end
\end{luacode*}
 
 
\begin{document}
 
{\tifont
\begin{longtable}{>{\color{black!50}\ttfamily\footnotesize}r|
                  *{10}{>{\color{black}}p{5em}|}}
\cline{2-11}
\endhead
 
\directlua{print_glyphs(10,65463)} \\ \cline{2-11}
\end{longtable}}
 
\end{document}

PDF

The next step then was to create a logic of how to address the different symbols. Of course TSX was helpful again (https://tex.stackexchange.com/questions/38402/how-to-pick-a-specific-symbol-from-a-specific-font):

\documentclass[twocolumn]{article}
\usepackage{fontspec}
\usepackage[top=4cm,left=2cm,right=2cm,bottom=2cm]{geometry}
\newfontfamily\tifont{BA2Plus Symbols}
\newcommand\tif[1]{{\tifont\symbol{#1}}}
\usepackage{fonttable}
 
\begin{document}
\twocolumn
 
33  >  \tif{33}
 
34  >  \tif{34}
 
...
 
124  >  \tif{124}
 
125  >  \tif{125}
 
\end{document}

PDF

The next step is to come up with a convenient way of addressing the glyphs, e.g. by creating aliases for each glyph.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Erste Schritte mit Python und Qt 5

Hier ein einfaches Beispiel, wie man mit Python und Qt 5 Desktop-Anwendungen bauen kann. Ich persönlich nutze Anaconda3, hier ist alles bei, was man dafür braucht.

Im Qt Designer (unter Anaconda3\Library\bin) erstellen wir ein neues GUI auf Basis von „Main Window“ und setzen einen Button und ein Label rauf. Das ganze wird dann abgespeichert, anschließend erstellen wir den notwendigen Python-Code.

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic
 
Ui_MainWindow, QtBaseClass = uic.loadUiType('test.ui')
 
class MyApp(QMainWindow):
    def __init__(self):
        super(MyApp, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.pushButton.clicked.connect(self.changeLabel)
 
    def changeLabel(self):
        self.ui.label.setText('Hello World')
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website