2

I defined the following simple macro:

\def\greek#1{
\symbol{\numexpr"03B0+#1}
}

This is supposed to convert numbers into greek characters for numbering purposes (so 1 becomes α, 2 becomes β etc.). It does its job, but for some weird reason, it always creates a whitespace of about 3pt behind the character, so that \greek{1}\greek{2}\greek{3} looks like this:

enter image description here

when it should actually look like this:

enter image description here

I know that I can get rid of that space by simply doing something like

\def\greek#1{
\symbol{\numexpr"03B0+#1}\hspace{-3.33pt}
}

but that seems to be a rather "hacked" solution. Is there any better way to do this? Why does it even do that anyway?

DLCom
  • 183
  • 6

1 Answers1

3

As David has explained in comments; TeX adds spaces at the end of lines. Hence your code was having SPACE<definition>SPACE. Many programmers prefer to code on multiple lines like in your example. To achieve that and still not have unwanted spaces; you can use the comment character of TeX at the end of lines in definition like this.

\documentclass{article}
\newcommand*{\greek}[1]{% Space ignored.
  \symbol{\numexpr"03B0+#1}% Space ignored.
}
\usepackage{fontspec}
\setmainfont{NewCM10-Regular.otf}

\begin{document} \greek{1}\greek{2}\greek{3} \end{document}

You will find this style of coding very commonly in the TeX-world.

You would need to use XeLaTeX/LuaLaTeX as this is Unicode.

Edit:

I have added the \numexpr part of the original answer back to my code after Enrico's remark in the comments. Apologies for the mistake.

Niranjan
  • 3,435
  • This only works if #1 is a single hexadecimal digit, which is probably not what the OP is looking for. – egreg Aug 28 '22 at 19:43
  • @egreg thanks. I will change the answer, but I was just wondering how \numexpr"03B0+10 becomes 03BA. What is the underlying magic that changes the B to C? – Niranjan Aug 29 '22 at 05:27
  • 1
    The argument to \symbol should be an integer; how it’s represented is irrelevant and \numexpr knows how to add numbers in different bases (decimal, octal, hexadecimal). ”03B0 is 944 and \symbol{“03BA} or \symbol{954} is the same. – egreg Aug 29 '22 at 07:36
  • @egreg Thanks for the explanation. I will remember this. – Niranjan Aug 29 '22 at 07:39