Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\documentclass{article}
\usepackage{CJKutf8}
\usepackage{ruby}
\renewcommand\rubysep{.05ex}
\renewcommand\rubysize{0.2}

\newcommand\神{\ruby{神}{かみ}}

\begin{document}
\begin{CJK}{UTF8}{min}
\神
\end{CJK}
\end{document}

How can I use Chinese characters (or more generally, Unicode characters) as a macro name?

I can use either pdflatex (preferred) or xelatex (the last resort).

share|improve this question
    
Try \@namedef and/or \@nameuse... –  Werner Jul 12 at 6:58
    
@Werner: How to use them? –  stalking is prohibited Jul 12 at 7:03
    
@FifaEarthCup2014 If foobar represents your desired macro name, you can define the macro with \@namedef{foobar}{<macro contents>} and \@nameuse{foobar} to call the macro. –  ChrisS Jul 12 at 7:18
5  
with tex/pdftex, basically you can't. with xetex or luatex you can, so you need to say what engine you are using. –  David Carlisle Jul 12 at 7:24
1  
@ChrisS except that you can't really do that here as in order to implement the utf-8 using 8bit fonts the characters need to be active and defined to expand to typesetting instructions so can not be used in \csname (\@namedef/\@nameuse) –  David Carlisle Jul 12 at 9:14

2 Answers 2

up vote 7 down vote accepted

run with xelatex or lualatex

\documentclass{article}
\usepackage{ruby}
\renewcommand\rubysep{.05ex}
\renewcommand\rubysize{0.2}
\usepackage{fontspec}
\setmainfont{Arial Unicode MS}

\newcommand\神{\ruby{神}{かみ}}

\begin{document}
\神
\end{document}

enter image description here

share|improve this answer

If you're using LuaTeX or XeTeX as engines you surely can define \神 to do that task. Of course you must remember to have a nonletter after \神 or to change the category code of to 12.

What happens with pdfTeX as engine? That \newcommand would give an error:

! LaTeX Error: Missing \begin{document}

because \newcommand can't define macros with parameter text not consisting of only #1, #1#2 and so on (see below).

Using \def\神{...} wouldn't help either: the input stream before tokenization would be the sequence of bytes

<5C><64><65><66><E7><A5><9E><7B>

that is, backslash, d, e, f , then the three bytes corresponding to the UTF-8 representation of (U+795E) and the open brace. What follows is not important for the topic because when TeX sees the open brace it knows that the parameter text has ended.

What you're really defining is, in fact, a control symbol, because the byte <E7> has been given category code 13 (active). This control symbol must be followed by the bytes (characters, if you prefer) <A5> and <9E>.

This wouldn't have adverse consequences, so long as you define no other similar combinations. For example, if you also do

\def\祟{...}

(with U+795F) you'll have a problem: this would be a definition of the control symbol \<E7>, so it would override the previous definition and usage of \神.

The fact is that what the editor shows you as a single character is, for TeX, formed by three bytes (characters). You might change the category code of <E7>, <A5> and <9E> to 11, so allowing to form a control word, but this would make it impossible to type and get the character, since CJKutf8 relies on <E7> to be active.


You may instead define a wrapper, say

\newcommand\CJKnewcommand[2]{% #1 is a character, #2 the replacement text
  \expandafter\newcommand\csname CJK++\detokenize{#1}++\endcsname{#2}%
}
\protected\def\?#1?{\csname CJK++\detokenize{#1}++\endcsname}

so after

\CJKnewcommand{神}{\ruby{神}{かみ}}

you might use

\?神?

Full example.

\documentclass{article}
\usepackage{CJKutf8}
\usepackage{ruby}
\renewcommand\rubysep{.05ex}
\renewcommand\rubysize{0.2}

\newcommand\CJKnewcommand[2]{% #1 is a character, #2 the replacement text
  \expandafter\newcommand\csname CJK++\detokenize{#1}++\endcsname{#2}%
}
\protected\def\?#1?{\csname CJK++\detokenize{#1}++\endcsname}


\CJKnewcommand{神}{\ruby{神}{かみ}}

\begin{document}
\begin{CJK}{UTF8}{min}
\?神?
\end{CJK}
\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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