2

Perhaps a question for David ?

I thought it would be interesting to be able to use unicode Greek characters directly to name nodes (points) with tkz-elements. I have no problem with the naming and all the macros seem to work correctly, but the problem arises with the tkzLabelPoint macro. As a first step, I've decided to create a new macro dedicated to Greek characters: \tkzLabelGPoint and I have a solution to automate this, but with the help of the luacode environment. I'd like to be able to use the directlua macro ... and I can't.

I hesitated about which question to ask. It's possible I've been wrong all along. Is it easier not to use lua?

\documentclass{article}
\usepackage[mini]{tkz-euclide}
\usepackage{tkz-elements}
\usepackage{luacode}

\begin{luacode}
-- Unicode Greek letter mapping to LaTeX
local greek_to_latex = {
  ["α"] = "\\alpha",
  ["β"] = "\\beta",
  ["γ"] = "\\gamma",
  ["δ"] = "\\delta",
  ["ε"] = "\\epsilon",
  ["ζ"] = "\\zeta",
  ["η"] = "\\eta",
  ["θ"] = "\\theta",
  ["ι"] = "\\iota",
  ["κ"] = "\\kappa",
  ["λ"] = "\\lambda",
  ["μ"] = "\\mu",
  ["ν"] = "\\nu",
  ["ξ"] = "\\xi",
  ["ο"] = "o",
  ["π"] = "\\pi",
  ["ρ"] = "\\rho",
  ["σ"] = "\\sigma",
  ["τ"] = "\\tau",
  ["υ"] = "\\upsilon",
  ["φ"] = "\\phi",
  ["χ"] = "\\chi",
  ["ψ"] = "\\psi",
  ["ω"] = "\\omega",
  -- Majuscules
  ["Α"] = "A",
  ["Β"] = "B",
  ["Γ"] = "\\Gamma",
  ["Δ"] = "\\Delta",
  ["Ε"] = "E",
  ["Ζ"] = "Z",
  ["Η"] = "H",
  ["Θ"] = "\\Theta",
  ["Ι"] = "I",
  ["Κ"] = "K",
  ["Λ"] = "\\Lambda",
  ["Μ"] = "M",
  ["Ν"] = "N",
  ["Ξ"] = "\\Xi",
  ["Ο"] = "O",
  ["Π"] = "\\Pi",
  ["Ρ"] = "P",
  ["Σ"] = "\\Sigma",
  ["Τ"] = "T",
  ["Υ"] = "\\Upsilon",
  ["Φ"] = "\\Phi",
  ["Χ"] = "X",
  ["Ψ"] = "\\Psi",
  ["Ω"] = "\\Omega"
}

function convert_greek_to_latex(char)
  local latex_char = greek_to_latex[char]
    tex.sprint("$" .. latex_char .. "$")
end
\end{luacode}

\newcommand{\convertGreek}[1]{%
  \directlua{convert_greek_to_latex("#1")}%
}

\def\tkzLabelGPoint[#1](#2){%
   \node[label style,#1] at (#2) {\convertGreek{#2}};
   }%
      
\begin{document}

Test: \convertGreek{α}, \convertGreek{β}

\vspace{12pt}
\begin{tikzpicture}
  \tkzDefPoint(1,2){α}
  \tkzDefPoint(3,1){β} 
  \tkzDrawLine(α,β)
  \tkzDrawPoints(α, β)
  \tkzLabelGPoint[red,below](α) 
  \tkzLabelGPoint[red,below](β)  
\end{tikzpicture}
\end{document}

enter image description here

So a direct answer to the question is

\documentclass{article}
\usepackage[mini]{tkz-euclide}
\usepackage{tkz-elements}

% stop expansion and use % not -- comments
\directlua{
%-- Unicode Greek letter mapping to LaTeX
local greek_to_latex = {
  ["α"] = "\string\\alpha",
  ["β"] = "\string\\beta",
  ["γ"] = "\string\\gamma",
  ["δ"] = "\string\\delta",
  ["ε"] = "\string\\epsilon",
  ["ζ"] = "\string\\zeta",
  ["η"] = "\string\\eta",
  ["θ"] = "\string\\theta",
  ["ι"] = "\string\\iota",
  ["κ"] = "\string\\kappa",
  ["λ"] = "\string\\lambda",
  ["μ"] = "\string\\mu",
  ["ν"] = "\string\\nu",
  ["ξ"] = "\string\\xi",
  ["ο"] = "o",
  ["π"] = "\string\\pi",
  ["ρ"] = "\string\\rho",
  ["σ"] = "\string\\sigma",
  ["τ"] = "\string\\tau",
  ["υ"] = "\string\\upsilon",
  ["φ"] = "\string\\phi",
  ["χ"] = "\string\\chi",
  ["ψ"] = "\string\\psi",
  ["ω"] = "\string\\omega",
  %-- Majuscules
  ["Α"] = "A",
  ["Β"] = "B",
  ["Γ"] = "\string\\Gamma",
  ["Δ"] = "\string\\Delta",
  ["Ε"] = "E",
  ["Ζ"] = "Z",
  ["Η"] = "H",
  ["Θ"] = "\string\\Theta",
  ["Ι"] = "I",
  ["Κ"] = "K",
  ["Λ"] = "\string\\Lambda",
  ["Μ"] = "M",
  ["Ν"] = "N",
  ["Ξ"] = "\string\\Xi",
  ["Ο"] = "O",
  ["Π"] = "\string\\Pi",
  ["Ρ"] = "P",
  ["Σ"] = "\string\\Sigma",
  ["Τ"] = "T",
  ["Υ"] = "\string\\Upsilon",
  ["Φ"] = "\string\\Phi",
  ["Χ"] = "X",
  ["Ψ"] = "\string\\Psi",
  ["Ω"] = "\string\\Omega"
}

function convert_greek_to_latex(char)
  local latex_char = greek_to_latex[char]
    tex.sprint("$" .. latex_char .. "$")
end
}

\newcommand{\convertGreek}[1]{%
  \directlua{convert_greek_to_latex("#1")}%
}

\def\tkzLabelGPoint[#1](#2){%
   \node[label style,#1] at (#2) {\convertGreek{#2}};
   }%
      
\begin{document}

Test: \convertGreek{α}, \convertGreek{β}

\vspace{12pt}
\begin{tikzpicture}
  \tkzDefPoint(1,2){α}
  \tkzDefPoint(3,1){β} 
  \tkzDrawLine(α,β)
  \tkzDrawPoints(α, β)
  \tkzLabelGPoint[red,below](α) 
  \tkzLabelGPoint[red,below](β)  
\end{tikzpicture}
\end{document}
1
  • 2
    -- Unicode Greek letter mapping to LaTeX do you really need to do that at all? it's probably simpler just to use the Greek in latex for which many solutions already exist, for example unicode-math for luatex or any of the Greek packages. Commented Feb 25 at 10:28

1 Answer 1

5

Especially in luatex but also in pdftex you don't need to convert α to \alpha to typeset it.

enter image description here

\documentclass{article}
\usepackage[mini]{tkz-euclide}
\usepackage{tkz-elements}
\usepackage{unicode-math}

\def\tkzLabelPoint[#1](#2){%
   \node[label style,#1] at (#2) {$#2$};%
   }%
 
\begin{document}

Test: $α$, $β$

\vspace{12pt}
\begin{tikzpicture}
  \tkzDefPoint(1,2){α}
  \tkzDefPoint(3,1){β} 
  \tkzDrawLine(α,β)
  \tkzDrawPoints(α, β)
  \tkzLabelPoint[red,below](α) 
  \tkzLabelPoint[red,below](β)
\end{tikzpicture}
\end{document}

if you do want to use the Lua you show in \directlua be aware that that expands commands so you would need \string\\alpha to stop \\ expanding to newline internals, or you could put the Lua in a lua file and then just use \directlua{require("your-lua.lua")} which is usually simplest for large blocks of code as it means the file is only read by Lua, not first by TeX, but here I don't think you need any Lua.

2
  • This is the first code I wrote and nothing happened. I forgot that I load unicode-math for my documentation but not for my examples. How stupid! That said, you might as well take advantage of your knowledge: what would it take to use \directlua with my horrible proposition? I had my doubts when I wrote : "I hesitated about which question to ask. It's possible I've been wrong all along. Is it easier not to use lua?" Commented Feb 25 at 11:10
  • 1
    @AlainMatthes as I wrote in the answer, you just need \string I put the full code into the answer Commented Feb 25 at 11:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.