4

I want to create an environment that would convert all á to \=a, all é to \=e and so on... (short-hand for macrons) Something that should have been a five-minutes easy-to-write tool became a real nightmare, mainly because I cannot activate the character á. TeX thinks it is actually two characters, creating an error with \catcode.

The following approach also does no work:

\defá{\=a}
\defé{\=e} 
...

Because the ''accent character'' comes before the ''letter character''. In this way, only ú works, while the rest yields an error.

Also, trying to isolate the ''accent character'' in a control sequence creates an inpuntec error (not including inputenc creates another error). But since inputenc itself does a similar trick quite well, I wonder: what is the nice way of doing it? And, what is better: a TeX solution or an inputenc contraption?

EDIT: The idea needs to be able to be executed in the body of a LaTeX. Global changes would mess up with my normal text, which uses quite a few diacritics (though no macrons).

  • You've marked this as tex-core, but the implication in the question is that you're using LaTeX and inputenc: is that the case? If so, could you tell us which encoding too! – Joseph Wright May 4 '14 at 18:24
  • Indeed I am using inputenc (utf8) with LaTeX, but I am mostly interested in how to active a character that I cannot use, or better, how to activate special characters. – pedro May 4 '14 at 18:28
  • Does this need to be done via \def or \newcommand (etc.)? It is not difficult to do this using non-*TeX tools... – jon May 4 '14 at 18:40
  • 3
    In utf8 à consist of two bytes. So for pdftex it looks like two chars. Use \DeclareUnicodeCharacter or the newunicodechar package to setup your commands. – Ulrike Fischer May 4 '14 at 19:11
  • 2
    inputenc's main role is to do what you describe, to make characters active and expand to definitions such as \'{e} what other definitions do you want other than the ones inputenc is making? – David Carlisle May 4 '14 at 20:20
3

In the UTF-8 encoding, ā is a two byte character; it is U+0101 and its UTF-8 representation is 0xC481, which means that the file has two bytes for representing the character. Remember that files on the computer are just sequences of bytes, which are interpreted in possibly different ways by the software.

TeX (in it's original form, which is shared by pdfTeX) is eight bit software and it doesn't really understand UTF-8, because it can read only one byte at a time. So, if your file is saved as UTF-8, an input that on the editor is shown as

\catcode`ā=\active

is really

\catcode`<C4><81>=\active

which is a syntax error. I use <xy> for representing a non ASCII character.

I don't understand why you'd want to change á to \=a, but it's your document, not mine. You have to go to low level programming. Here's an example:

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}
This is an acute a: á

Now for something completely different:
\begingroup
\expandafter\def\csname u8:\detokenize{á}\endcsname{\=a}%
á
\endgroup
\end{document}

This prints

enter image description here

So you see that what you want is possible. Now, let's build an interface for this.

\documentclass{article}
\usepackage[utf8]{inputenc}

\makeatletter
\newcommand{\convertaccents}[1]{%
  \@for\next:=#1\do{\expandafter\convert@accents\next}%
}
\newcommand\convert@accents[2]{%
  \expandafter\def\csname u8:\detokenize{#1}\endcsname{#2}%
}
\makeatother

\newenvironment{convertedtext}[1]
  {%
   \convertaccents{#1}
   \begin{quote}% or whatever
  }
  {%
   \end{quote}
  }

\begin{document}
This is an acute a: á

Now for something completely different:
\begin{convertedtext}{{á}{\=a},{é}{\=e}}
áé
\end{convertedtext}
\end{document}

Here I give the list of characters to change as an argument to the environment. It's not mandatory, you can give the list as part of \newenvironment; it mostly depends on what you have to do.

enter image description here

  • Thank you for the solution! Exactly what I was looking for. – pedro May 4 '14 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.