3

I am trying to make a function \noteunic with fontspec and xelatex that allows me to write with a unicode font for a limited section of the text only. But when I write this, the font leaks outsite the domain it is supposed to be limited to.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}

\usepackage{fontspec} % 
\newfontfamily\noteunic[BoldFont=meiryob.ttc]{meiryo.ttc}
\newcommand\textunic[1]{\noteunic {#1}}

\begin{document}
Main font \\
\noteunic{Something} \\
Not main font
\end{document}

What am I doing wrong ?

3
  • 1
    Did you mean to put \textunic in the body of the document?
    – Thruston
    Sep 6, 2015 at 13:59
  • Don't use inputenc if you are compiling with XeLaTeX. Also, use \DeclareTextFontCommand rather than \newcommand. But none of this is the issue here: @Thruston already identified that.
    – cfr
    Sep 6, 2015 at 14:01
  • I used \DeclareTextFontCommand{\textunic}{\noteunic} instead, but no idea what different does it make. I kind of feel stupid now, but yes the problem was that I have not been using the same function. Thanks Thruston.
    – HcN
    Sep 6, 2015 at 14:10

1 Answer 1

5

The code

\newfontfamily\noteunic[BoldFont=meiryob.ttc]{meiryo.ttc}

defines \noteunic a declaration, that is, a command whose effect ends only with the group which the command appears in. Not as a command with an argument; the same holds for \bfseries, \itshape,\large` and the like.

So if you type

\noteunic{Abc}

you're not saying that only Abc should be printed in the meiryo font, but “start using meiryo from this point on”. However, an end of group will turn off the font selection. Thus the code should be

{\notenuic Abc}

and the } will effectively turn off the font selection.

In the case above, Abc is enclosed in a group, but this group does nothing. With your definition of \textunic, the code \textunic{Abc} would become \noteunic{Abc} because of macro replacement, which causes the “leaking” as seen.

LaTeX provides an elegant way for defining the “argument” version for a font change declaration:

\DeclareTextFontCommand{\textunic}{\noteunic}

and with this in the preamble the code

\textunic{Abc}

will do the expected thing.

You must log in to answer this question.

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