8

Is it possible a command like

\char{n}

where n is a number between 0 and 255? For example

\char{241}

returns ñ as output. I searched the web for something similar, even within this site, but I did not find anything

user3204810
  • 1,415
  • Please clarify what you're trying to achieve. For sure, there is an instruction called \char. – Mico Mar 19 '18 at 16:26
  • What I'm trying to achieve: a command to print characters by his ASCII code. e.g. \mycommand{142} prints Ž – user3204810 Mar 19 '18 at 16:28
  • 3
    I don't understand. ASCII stops at 128, iirc. And doesn't contain many accented letters. – Johannes_B Mar 19 '18 at 16:30
  • The extended ASCII ends at 255, or not? – user3204810 Mar 19 '18 at 16:34
  • Assuming you've loaded the fontenc package with the option T1, typing either \char0241 or \char241 prints out, you guessed it, ñ. A full MWE: \documentclass{article} \usepackage[T1]{fontenc} \begin{document} \char0241 \end{document}. Is that what you're trying to achieve? – Mico Mar 19 '18 at 16:35
  • Yes, exaclty. I did not know of the existence of this command – user3204810 Mar 19 '18 at 16:38

2 Answers2

13

There is not anything called "the extended ascii" there are hundreds of ascii extensions "code pages" where the values between 128 and 256 might be accented latin letters as you have, or cyrillic, or Greek, or different accented letters depending on the encoding. So in latex you can use \symbol{241} (the latex syntax for \char241) but it depends on the encoding of the font you are using so normally you would never do that in a document.

If you use \symbol{241} in the body of the document but then change your font setup in the preamble you may get a different character with no warning or no character at all. The whole point about latex's encoding dependent commands like \~{n} is that the markup produces the correct character whatever numbers are used to access that character in the font being used.

David Carlisle
  • 757,742
  • May I humbly ask why this doesn't work on Math.Stackexchange? Are they using a special flavor of Latex that is more light-weight? – aderchox Mar 01 '21 at 09:07
  • 1
    @aderchox they do not use latex at all, the "latex" rendering on most websites is mathjax which is javascript not tex. It emulates a subset of user-level latex math syntax but does not implement non-math commands like \char at all and doesn't implement any of the lower level tex commands that you might find in definitions on this site. – David Carlisle Mar 01 '21 at 15:24
7

The \char macro is somewhat special in that it does not expect its argument to be enclosed in curly braces. If \char is followed by a number (say, 241), this number is taken to denote a base-10 number. If you write \char"F1, i.e., if you insert " after \char, the number (F1) will be interpreted to denote a hexadecimal number.

A full MWE:

\documentclass{article} 
\usepackage[T1]{fontenc} 
\begin{document} 
\char0241 \char"F1 % both will print out "ñ"
\end{document} 
Mico
  • 506,678