5

In the ASCII table, …

  • ! (exclamation mark) is a printable character and its decimal representation is 33
  • a (latin small letter a) is also a printable character and its decimal representation is 97
  • } (right curly bracket) is also a printable cahracter and its decimal representation is 125

I want to know if there's a macro in LaTeX that converts a decimal representation to its ASCII character.

\symbol{...} returns a for 97 and ! for 33. I thought that \symbol{...} accomplished what I am looking for, but it returns ˝ (double acute accent) for 125.

\documentclass{article}

\begin{document}
\symbol{33} % exclamation mark
\symbol{97} % latin small letter a
\symbol{125} % it should return "right curly bracket", but it returns ˝ (double acute accent)
\end{document}

enter image description here

My question is: In the context of LaTeX, is there a built-in macro to convert an integer (i.e. decimal representation) to its ASCII character? The macro should work for any printable character in ASCII (i.e. between the range 32 to 126.)

I found this question which is similar to my question, but it was conflated with the definition of "extended ascii table".

2
  • 1
    It can't work if the symbol is not available in the current font encoding. That would be rare for these characters, but far from impossible.
    – cfr
    Commented Mar 27 at 6:50
  • The verb “return” is quite ambiguous, being TeX both a (weird) programming language and a typesetting system. Do you intend “return” as “to put back the character in the input stream for further processing” or “typeset the character having that ASCII code”?
    – egreg
    Commented May 15 at 8:38

3 Answers 3

12

The TeXbook says

To get access to any character whatsoever, you can type

\char⟨number⟩

where ⟨number⟩ is any number from 0 to 255 (optionally followed by a space); you will get the corresponding character from the current font.

The default font is cmr10, in which the font character in position 125 ('175 in octal notation) is ̋ . That's why \char125 and \symbol{125} return ̋ instead of right curly brace.

However, the cmtt10 font is consistent with ASCII table (To confirm this, have a look at page 429 of the TeXbook). So, in order to have \char125 produce the right curly brace, you can choose to use cmtt10 font:

\documentclass{article}
\begin{document}

\texttt{\char33\char37\char97\char125\symbol{125}}

\end{document}

enter image description here

Another method is using T1 font encoding, i.e., adding

\usepackage[T1]{fontenc}

in the preamble.

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}

\char33\char37\char97\char125\symbol{125}

\end{document}

enter image description here

5

The TeX primitive \char number creates a character with given code number. The TeX primitive \Uchar number creates a token with given code number. The second one is implemented only in XeTeX and LuaTeX. The difference is: \char number works after tokenization (in XeTeX or LuaTeX, the Unicode is accepted) and it is not expandable, \Uchar number returns a Unicode character: token of category 12 and it is expandable.

4

As wipet says, at the primitive level, classical engines do not offer a way to generate a character by expansion. However, this can be done using macros: in expl3 there is \char_generate:nn for this purpose, or for more general codepoints (full Unicode coverage), \codepoint_generate:nn. We might therefore do

\ExplSyntaxOn
\cs_new:Npn \mysymbol #1 { \codepoint_generate:nn {#1} { 12 } }
\ExplSyntaxOff

to create a command \mysymbol that makes 'other' characters across the Unicode range by expansion.

Note that as Stephen says, this doesn't mean that a symbol will be printable directly.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .