# UTF-8 (but not in current font) character in newcommand

I'm trying to define a new single-character LaTeX command (to insert a 1 pt space with \kern1pt) and want to use one of the characters obtained through the AltGr key on a US International keyboard layout. I've chosen the Unicode U+00A6 '¦', aka 'Broken Bar' character. This is my preamble

\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{babel}


and this is my newcommand definition

\expandafter\newcommand\csname u8:\detokenize{¦}\endcsname{\kern1pt}


and the error I get:

! LaTeX Error: Command \u8:┬ª already defined.
.              Or name \end... illegal, see p.192 of the manual.


So, trying to understand what is going on, I replaced '¦' with '∙', the Unicode U+2219 'Bullet Operator' character. I first made sure that LaTeX complained, in insertions in normal text, as loudly with '∙' as it does with '¦', (it does) and then changed my newcommand definition thus:

\expandafter\newcommand\csname u8:\detokenize{∙}\endcsname{\kern1pt}


This time I got no errors, so I proceeded to use my new macro

a\∙b


and got this error:

! Undefined control sequence.
l.530 a\Ô
.        êÖb


Trying to fix that, I added this to my preamble,

\DeclareUnicodeCharacter{2219}{\textsurd}    % Recognise ∙' character in input


and got this error instead:

! LaTeX Error: Command \u8:ÔêÖ already defined.
.              Or name \end... illegal, see p.192 of the manual.


Can someone please save me hours sifting through the newunicodechar package manual? (And I remind that it's a '¦', not '∙' that I need to use.)

-

## 4 Answers

There are a couple of problems:

1. There is already an action defined for ¦, precisely \IeC{\textbrokenbar}, which is kind of expected; thus \newcommand will give you the error.

2. If you do

\expandafter\newcommand\csname u8:\detokenize{∙}\endcsname{\kern1pt}


you're not defining the macro \∙, but a meaning for the Unicode character ∙. Since ∙ is represented in UTF-8 by the triple E2 88 99, TeX will see \^^e2 and the error message uses some representation of the three bytes.

With newunicodechar you don't have to do anything special:

% -*- coding: utf-8 -*-
\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{babel}
\usepackage{newunicodechar}
\newunicodechar{¦}{\kern20pt} % exaggerated to show the effect
\begin{document}
A¦A
\end{document}


The output is

and the log file will report

Package newunicodechar Warning: Redefining Unicode character on input line 11.


which would be

Package newunicodechar Warning: Redefining Unicode character; it meant
(newunicodechar)                ***  \IeC {\textbrokenbar }  ***
(newunicodechar)                before your redefinition on input line 11.


if the verbose option is used (\usepackage[verbose]{newunicodechar}).

Here's the relevant part from the documentation of newunicodechar.

The package provides only one command, \newunicodechar, which must be called with two arguments:

\newunicodechar{<char>}{<code>}

where <char> is the Unicode character to which we need to give a meaning and <code> is that meaning, that is the LaTeX code that will be substituted to the character.

-
Many thanks, egreg. The first thing is that I must learn to check the log file when I get errors. (I'm afraid I don't, instead trying all sort of things to fix it.) The other is that I had no idea you could define a macro in the {<code>} part of the \newunicodechar command. Then again (as I admitted in my question) I haven't read enough of the package doc. –  peter.cntr Mar 11 '13 at 21:27
@peter.cntr The <code> part is pretty much arbitrary; don't use the same <char> in it, of course, as it would produce an infinite loop. For instance, by doing \newunicodechar{¦}{} (with the verbose option) you can see the original definition (if any) and use that code in case you need to supplement it; so, to add a bit of space around the broken bar, you might do \newunicodechar{¦}{\kern1pt\IeC{\textbrokenbar}\kern1pt}. –  egreg Mar 11 '13 at 21:31
Got that, egreg. Very useful. And using the verbose option is something eles I've got to think of when I'm in a pickle. –  peter.cntr Mar 11 '13 at 21:38

If all you want is a solution to your problem:

\expandafter\renewcommand\csname u8:\detokenize{¦}\endcsname{\kern1pt}


Now some explanations.

Dealing with utf-8 in tex is tricky. For TeX one byte is one character, while in utf8 some characters require more bytes (see my answer to Catcodes of unicode characters with \usepackage[utf8]{inputenc} for more details)

In your case, unicode character ¦ is encoded in utf8 as a sequence of two bytes (hex values C2 A6), but for TeX those are two separate chars. Those bytes, incidentally encode the glyphs ┬ª in the codepage used by old windows terminals, which explains part of your error message, in a linux terminal using latin-1 you would have seen Â¦ instead).

Using \csname u8:\detokenize{¦}\endcsname you are writting a TeX macro name untypeable otherwise which is \u8:┬ª. inputenc also does other tricks to make some bytes "active chars" (byte C2 among them) so that when they are found in the input they trigger some of inputenc internal macros.

In this particular case, when the input contains the sequence (as seen by tex) C2 A6, inputenc manages the things to invoke the macro \u8:┬ª Unfortunately textcomp package already defined that macro to typeset a broken bar (the expected result), so your attempt to define it breaks. My solution of using \renewcommand works as expected.

Your second attempt using · worked, apparently that sequence was not defined. Character · is encoded in utf8 as the sequence C2 B7 (┬À in your terminal). But then, when you try to use it by writing \·, what TeX sees is \┬À and that macro is not defined. The macro you defined is \u8:┬À. To get this macro called you have to write only · in your source, and not \·.

However, for reasons beyond my understanding, when I type A·B, although all compiles without errors, I get A·B in the PDF, instead of the desired kerning. This happens only with · char. Other "rare" characters I tried, such as § works perfectly.

-
Thanks, but actually, I had tried that two (but didn't say so as my question is long enough as it is!) I get the 'Undefined control sequence error' just for defining the new macro. In the immediate, I just want it to work! –  peter.cntr Mar 11 '13 at 20:09
Many thanks for you very detailed answer, JLDiaz. I had posted my previous comment after only reading your short one. It now seems to me that trying to use ¦ is really seeking trouble! What you said about the textcomp package did ring a bell. As it happens, I'm just using \newcommand{\kp}{\kern1pt}, which of course works fine and is only two characters. Trouble is, it affect legibility in the middle of complex equations... –  peter.cntr Mar 11 '13 at 20:16
@peter.cntr I think that you were trying to use Foo\¦bar instead of simply Foo¦bar (since you did this mistake also in your \· example. I tried to explain why that won't work, but apparently I failed :-) –  JLDiaz Mar 12 '13 at 0:42
I did! -- try to use Foo\¦bar -- instead of simply Foo¦bar -- i.e., I was trying to define a macro with a one-character name -- namely, the character '¦'. I thought that redefining a character as a command was very 'low-level' stuff, not accessible (easily, anyway) in LaTeX. Now I've learned a lot (and have a lot more to learn...) Many thanks for your efforts! –  peter.cntr Mar 12 '13 at 6:32

There is no need to use the internal csname form:

\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{babel}

\DeclareUnicodeCharacter{00A6}{((\kern1pt))}
\begin{document}

a [¦] b

\end{document}


produces

where the (()) are just to make things visible

-
Fabulous, David! Would never had thought one could do that with \DeclareUnicodeCharacter... It doesn't explain why LaTeX complains about the other attempted solutions, but this a much nicer one: works perfectly! –  peter.cntr Mar 11 '13 at 20:54
@peter.cntr well you get an error with \newcommand because the default utf8 setup already defines this command (to make a broken bar) didn't look too closely at the later errors. –  David Carlisle Mar 11 '13 at 20:58
Makes sense. And since I'm fully happy at the moment with your solution, I'll leave (for the time being) trying to understand why the error also occurs with \renewcommand. –  peter.cntr Mar 11 '13 at 21:08

I can't reproduce your problem with \DeclareUnicodeCharacter. It works fine for me

\documentclass[11pt,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{babel}
\DeclareUnicodeCharacter{00A6}{hallo}
\DeclareUnicodeCharacter{2219}{World}
\begin{document}
¦ ∙
\end{document}
`
-
My problem did not occur with \DeclareUnicodeCharacter, Ulrike, but when I tried to define a macro named '\¦'. –  peter.cntr Mar 11 '13 at 20:58