| 1 |
% Copyright 2019 by Mark Wibrow |
| 2 |
% |
| 3 |
% This file may be distributed and/or modified |
| 4 |
% |
| 5 |
% 1. under the LaTeX Project Public License and/or |
| 6 |
% 2. under the GNU Free Documentation License. |
| 7 |
% |
| 8 |
% See the file doc/generic/pgf/licenses/LICENSE for more details. |
| 9 |
|
| 10 |
|
| 11 |
\section{Babel Library} |
| 12 |
\label{section-library-babel} |
| 13 |
|
| 14 |
\begin{tikzlibrary}{babel} |
| 15 |
A tiny library that make the interaction with the |babel| package easier. |
| 16 |
Despite the name, it may also be useful in other contexts, namely whenever |
| 17 |
the catcodes of important symbols are changed globally. Normally, using |
| 18 |
this library is always a good idea; it is not always loaded by default |
| 19 |
since in some rare cases it may break old code. |
| 20 |
\end{tikzlibrary} |
| 21 |
|
| 22 |
The problems this library tries to fix have to do with the so-called |
| 23 |
``catcodes'' of symbols used inside \tikzname. In normal \TeX\ operation, |
| 24 |
symbols like |!| or |"| are ``normal'' characters and the \tikzname\ parser |
| 25 |
expects them to be. Some packages, most noticeably the |babel| package, |
| 26 |
aggressively change these character codes so that for instance a semicolon gets |
| 27 |
a little extra space in |french| mode or a quotation mark followed by a |
| 28 |
vertical bar breaks ligatures in |german| mode. |
| 29 |
|
| 30 |
Unfortunately, \tikzname\ expects the character codes of some symbols to be |
| 31 |
``normal''. In some important cases it will tolerate changed character codes, |
| 32 |
but when the changes made by |babel| (or some other package) are too |
| 33 |
``aggressive'', compilation of \tikzname\ code will fail. |
| 34 |
|
| 35 |
The |babel| library of \tikzname\ is intended to help out in this situation. |
| 36 |
All this library does is to set the following two keys to |true|. You can, |
| 37 |
however, also set these keys directly and also switch them off or on |
| 38 |
individually and independently of this library. |
| 39 |
|
| 40 |
\begin{key}{/tikz/handle active characters in code=\opt{\meta{true or false}} (initially false)} |
| 41 |
When this key is set, at the beginning of every |\tikz| command and every |
| 42 |
|{tikzpicture}|, the character codes of all symbols used by \tikzname\ are |
| 43 |
reset to their normal values. Furthermore, at the beginning of each node, |
| 44 |
the catcodes are restored to the values they had prior to the current |
| 45 |
picture. |
| 46 |
|
| 47 |
The net effect of this is that, in most cases, symbols having a special |
| 48 |
character code can be used nicely both in \tikzname\ code and also in node |
| 49 |
texts. |
| 50 |
|
| 51 |
In the following, slightly silly, example we make the dot an active |
| 52 |
character and define it in some strange way. Now, in the later \tikzname\ |
| 53 |
command, the dot in |3.0cm| may no longer be active and setting the |
| 54 |
|handle...| option achieves exactly this. However, as can be seen, the dot |
| 55 |
is once more active inside the node. |
| 56 |
% |
| 57 |
\begin{codeexample}[] |
| 58 |
\catcode`\.=\active |
| 59 |
\def.{\o} |
| 60 |
|
| 61 |
\tikz [handle active characters in code] |
| 62 |
\node [draw, minimum width=3.0cm] {hall. pe.ple}; |
| 63 |
\end{codeexample} |
| 64 |
% |
| 65 |
\end{key} |
| 66 |
|
| 67 |
\begin{key}{/tikz/handle active characters in nodes=\opt{\meta{true or false}} (initially false)} |
| 68 |
This key is needed for a special situation: As explained for the |
| 69 |
|handle ... code| key, that key switches off all special meaning of symbols |
| 70 |
and switches them back on again at the beginning of nodes. However, there |
| 71 |
is one situation when this is not possible: When some text has already been |
| 72 |
read by \TeX, the catcodes can no longer change. Now, for normal nodes this |
| 73 |
is not a problem since their contents has not been read at the moment the |
| 74 |
catcodes are restored. In contrast for label nodes for edges, nodes |
| 75 |
produced by the |graph| and |quotes| libraries, and some others nodes, |
| 76 |
their text \emph{has} already been read when the catcodes get adjusted. |
| 77 |
|
| 78 |
The present key may help in such situations: It causes the text of all such |
| 79 |
``indirectly created'' nodes to be surrounded by a call to the |
| 80 |
|\scantokens| command. This command attempts to reread an already read |
| 81 |
text, but allows catcodes to change. As users of this command will know, it |
| 82 |
is not a perfect substitute for directly reading the text by \TeX, but it |
| 83 |
normally has the desired effect. |
| 84 |
% |
| 85 |
\begin{codeexample}[] |
| 86 |
\catcode`\.=\active |
| 87 |
\def.{\o} |
| 88 |
|
| 89 |
\tikz [handle active characters in code, |
| 90 |
handle active characters in nodes] |
| 91 |
\node [draw, label=f..] {hall. pe.ple}; |
| 92 |
\end{codeexample} |
| 93 |
% |
| 94 |
\end{key} |