| 1 |
|
| 2 |
% cnv.tex -- macro for string conversion |
| 3 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 4 |
% April 2005 Petr Olsak |
| 5 |
|
| 6 |
|
| 7 |
% \cnvin {<input text>} converts <input text> to <output text>. |
| 8 |
% This output is stored in \cnvout macro. |
| 9 |
|
| 10 |
% For more information see the end of this file. |
| 11 |
% The comments after %% (double percent sign) are intended for macro |
| 12 |
% programmers. See "simple" documentation at the end of this file first. |
| 13 |
|
| 14 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 |
|
| 16 |
%% we cannot read this macro twice: |
| 17 |
|
| 18 |
\ifx\cnvin\undefined \else \endinput \fi |
| 19 |
|
| 20 |
%% \predef <token> defines macro \E:<token>:<table> |
| 21 |
%% \findef <token> defines macro \T:<token>:<table> |
| 22 |
|
| 23 |
\def\predef #1{\expandafter \predefmethod \csname E:\string#1:\cnvtable\endcsname} |
| 24 |
\def\findef #1#2{\expandafter \findefmethod \csname T:\string#1:\cnvtable\endcsname {#2}} |
| 25 |
|
| 26 |
%% \cnvaccent <command> <char> {<output>} does following: |
| 27 |
%% 1. it defines \E:<command>:<table> #1 as {\runcnvaccent \A:<command>:#1:<table>} |
| 28 |
%% (\runcnvaccent implements the test if accented letter is undeclared, see below) |
| 29 |
%% 2. it defines \A:<command>:<char>:<table> as {<output>} |
| 30 |
%% 3. if "<command> <char>" is one <token> after expansion, then |
| 31 |
%% it defines \T:<token>:<table> as {<output>} |
| 32 |
|
| 33 |
\def\cnvaccent #1#2#3{\expandafter \cnvaccentmethod \csname E:\string#1:\cnvtable\endcsname ##1% |
| 34 |
{\cnvexpand \expandafter \runcnvaccent \csname A:\string#1:\string##1:\cnvtable\endcsname}% |
| 35 |
\expandafter \findefmethod \csname A:\string#1:\string#2:\cnvtable\endcsname {#3}% |
| 36 |
\edef \tmp{#1{#2}}% |
| 37 |
\expandafter \cnvtestaccent \tmp \cnvtestaccent |
| 38 |
\ifx\nextchar\empty \expandafter \findefmethod \csname T:\tmp:\cnvtable\endcsname {#3}\fi |
| 39 |
} |
| 40 |
\def\cnvtestaccent {\futurelet \nextchar \docnvtestaccent} |
| 41 |
\def\docnvtestaccent #1#2\cnvtestaccent{\ifx\nextchar \bgroup |
| 42 |
\else \def\nextchar{#2}\fi} |
| 43 |
|
| 44 |
%% main macro: |
| 45 |
|
| 46 |
\long\def\cnvin #1{\def\cnvout{}\cnvnext #1\cnvend} |
| 47 |
|
| 48 |
%% \cnvnext: we test the next token by \futurelet because it may be a space |
| 49 |
%% or a brace: |
| 50 |
|
| 51 |
\def\cnvnext {\futurelet \nextchar \docnvnext} |
| 52 |
\def\docnvnext {\let\next=\cnvtoken |
| 53 |
\ifx \nextchar\cnvend \let\next=\relax \fi |
| 54 |
\ifx \nextchar\spacetoken \let\next=\cnvspace \fi |
| 55 |
\ifx \nextchar\cnvbgroup \let\next=\cnvopenbrace \fi |
| 56 |
\ifx \nextchar\cnvegroup \let\next=\cnvclosebrace \fi |
| 57 |
\ifx \nextchar\runcnvaccent \let\next=\relax \fi |
| 58 |
\ifx \nextchar\cnvexec \let\next=\relax \fi |
| 59 |
\ifx \nextchar\cnvexpand \let\next=\relax \fi |
| 60 |
\ifx \nextchar\cnvexpandtext \let\next=\relax \fi |
| 61 |
\next} |
| 62 |
|
| 63 |
%% \cnvend: end of conversion. We define it as \outer because |
| 64 |
%% the parameter of user macros cannot scan this token. |
| 65 |
|
| 66 |
\outer\def\cnvend{\let\nextchar=\relax} |
| 67 |
|
| 68 |
%% \cnvbgroup, \cnvegroup: We suppose that these sequences will have |
| 69 |
%% the constant meaning (user may change the \bgroup, \egroup meaning). |
| 70 |
|
| 71 |
\let\cnvbgroup={ \let\BGROUP={ \let\EGROUP=} \let\cnvegroup=} |
| 72 |
|
| 73 |
%% \spacetoken is the control sequence equal to space token (catcode 10): |
| 74 |
|
| 75 |
{\def\\{\global\let\spacetoken= }\\ } |
| 76 |
|
| 77 |
%% \cnvtoken: next <token> is scannable in #1 parameter |
| 78 |
%% It expands to \E:<token>:<table> (if defined) else |
| 79 |
%% it stores contents of \T:<token>:<table> to \cnvout (if defined) else |
| 80 |
%% it stores <token> to \cnvout using \cnvdefault macro. |
| 81 |
|
| 82 |
\def\cnvtoken #1{\def\tmpc{#1}\docnvtoken} |
| 83 |
\def\docnvtoken{% |
| 84 |
\expandafter \ifx \csname E:\expandafter\string\tmpc:\cnvtable\endcsname \relax |
| 85 |
\expandafter \ifx \csname T:\expandafter\string\tmpc:\cnvtable\endcsname \relax |
| 86 |
\ifx\tmpc\space \cnvstoree{\expandafter \cnvout \tmpc}% |
| 87 |
\else \cnvstoreee {\expandafter \expandafter \expandafter \cnvout |
| 88 |
\expandafter \cnvdefault \tmpc}% |
| 89 |
\fi |
| 90 |
\else |
| 91 |
\cnvstoreee {\expandafter \expandafter \expandafter |
| 92 |
\cnvout \csname T:\expandafter\string\tmpc:\cnvtable\endcsname}% |
| 93 |
\fi |
| 94 |
\expandafter \cnvnext |
| 95 |
\else |
| 96 |
\expandafter \expandafter \expandafter \cnvnext |
| 97 |
\csname E:\expandafter\string\tmpc:\cnvtable\expandafter \endcsname |
| 98 |
\fi} |
| 99 |
|
| 100 |
%% \cnvspace, \cnvopenbrace, \cnvclosebrace: the next token is unreadable |
| 101 |
%% by #1 macro parameter. We define \tmpc, remove the next token |
| 102 |
%% and run \docnvtoken macro. |
| 103 |
|
| 104 |
\def\cnvspace {\def\tmpc{ }\afterassignment \docnvtoken \let\next= } |
| 105 |
\def\cnvopenbrace {\def\tmpc{\BGROUP}\afterassignment \docnvtoken \let\next= } |
| 106 |
\def\cnvclosebrace {\def\tmpc{\EGROUP}\afterassignment \docnvtoken \let\next= } |
| 107 |
|
| 108 |
%% \runcnvaccent: next token #1 is \A:<command>:<char>:<table>. |
| 109 |
%% It stores the contents of \A:<command>:<char>:<table> to \cnvout |
| 110 |
%% (if defined) else it stores \cnvadefault to \cnvout. |
| 111 |
|
| 112 |
\def\runcnvaccent #1{\ifx #1\relax |
| 113 |
\cnvstoree {\expandafter \cnvout \cnvadefault}% |
| 114 |
\else |
| 115 |
\cnvstoree {\expandafter \cnvout #1}% |
| 116 |
\fi \cnvnext} |
| 117 |
|
| 118 |
%% \cnvexec: we switch off the conversion until \cnvnext |
| 119 |
%% The contents of this macro means: do nothing, but we need |
| 120 |
%% somewhat special contents in order to distinguish it by \ifx. |
| 121 |
|
| 122 |
\long\def\cnvexec {\empty\relax\relax} |
| 123 |
|
| 124 |
%% \cnvexpand, \cnvexpandtext: see the documentation at the end of this file |
| 125 |
|
| 126 |
\def\cnvexpand {\expandafter \cnvnext} |
| 127 |
\def\cnvexpandtext #1{\edef\tmp{#1}\expandafter \cnvnext \tmp} |
| 128 |
|
| 129 |
%% \cnvstoreee: macro appends the desired contens to \cnvout. |
| 130 |
%% We cannot expand the \cnvout by \edef because the expandable tokens can be |
| 131 |
%% included here. So we need to do some \expandafter gymnastics. |
| 132 |
|
| 133 |
\def\cnvstore {\expandafter \def \expandafter \cnvout \expandafter} |
| 134 |
\def\cnvstoree {\expandafter \cnvstore \expandafter} |
| 135 |
\def\cnvstoreee {\expandafter \cnvstoree \expandafter} |
| 136 |
|
| 137 |
%% Implicit values: |
| 138 |
|
| 139 |
\def\cnvcopy{} |
| 140 |
\let\cnvdefault=\cnvcopy |
| 141 |
\def\cnvadefault{?} |
| 142 |
\def\cnvtable{} |
| 143 |
|
| 144 |
\let\predefmethod=\gdef |
| 145 |
\let\findefmethod=\gdef |
| 146 |
\let\cnvaccentmethod=\gdef |
| 147 |
|
| 148 |
\endinput |
| 149 |
|
| 150 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 151 |
|
| 152 |
---> english documentation: see the end of this file |
| 153 |
|
| 154 |
Po provedení příkazu \cnvin {<seznam tokenů>} |
| 155 |
je v makru \cnvout ukryt konvertovaný seznam tokenů. |
| 156 |
|
| 157 |
Konverze se provádí bez obvyklé expanze <seznamu tokenů> a je jedno, jaké |
| 158 |
kategorie jednotlivé tokeny mají. Konverze probíhá ve dvou průchodech: |
| 159 |
|
| 160 |
1. Kontrolovaná částečná expanze jednotlivých tokenů. Po jejím |
| 161 |
provedení se konverze vrací k výsledku expanze a konvertuje jej |
| 162 |
znovu. Tokeny, které takto expandují, se v konverzní tabulce |
| 163 |
deklarují pomocí \predef. Tokeny, které nejsou takto deklarovány, |
| 164 |
při konverzi neexpandují (ačkoli při běžné expanzi třeba expandují). |
| 165 |
2. Finální konverze tokenu: token je konvertován podle deklarace |
| 166 |
makrem \findef na výstupní posloupnost tokenů, ke kterým se už |
| 167 |
konverze nevrací. |
| 168 |
|
| 169 |
Tokeny, které podléhají konverzi, mohou mít libovolnou kategorii. Není |
| 170 |
tedy nutné, aby se jednalo o kontrolní sekvence. |
| 171 |
|
| 172 |
Deklarace tokenů pomocí \predef a \findef jsou zcela nezávislé na |
| 173 |
"skutečném významu" tokenu. Jedná-li se o kontrolní sekvenci, bude |
| 174 |
se tato kontrolní sekvence při běžném zpracování \TeX{}em chovat |
| 175 |
běžným způsobem ačkoli má pro konverzi deklarován pomocí \predef nebo |
| 176 |
\findef odlišný význam. |
| 177 |
|
| 178 |
Deklarace pomocí \predef může mít běžné parametry a separátory (#1#2 atd.) |
| 179 |
|
| 180 |
Každý token může mít jen jeden význam deklarovaný pomocí \predef nebo |
| 181 |
\findef. Je-li token deklarován obojím způsobem, má při konverzi přednost |
| 182 |
\predef. Je-li token deklarován opakovaně stejným deklarátorem, platí |
| 183 |
poslední deklarace. |
| 184 |
|
| 185 |
Pokud se má konvertovat token, který nemá žádnou deklaraci, pak je |
| 186 |
výsledek závislý na hodnotě makra s jedním parametrem \cnvdefault. |
| 187 |
Implicitně je \cnvdefault definováno tak, že se nedeklarovaný token |
| 188 |
kopíruje beze změny do výstupu. Uživatel má tyto další možnosti: |
| 189 |
|
| 190 |
\def\cnvdefault #1{<out>} % každý nedeklarovaný token se konvertuje |
| 191 |
% na společný <out> |
| 192 |
\def\cnvdefault #1{} % nedeklarovaný token tiše zmizí |
| 193 |
|
| 194 |
Příklad hypotetické konverzní tabulky: |
| 195 |
|
| 196 |
\predef \TeX {TeX} |
| 197 |
\predef \uv #1{\leva #1\prava} |
| 198 |
\predef X{AB} |
| 199 |
\findef A {:XX:\TeX:} |
| 200 |
\cnvin{Tady je \uv{\TeX}} |
| 201 |
|
| 202 |
V \cnvout máme text: "Tady je \leva Te:XX:\TeX:B\prava". Obsah \cnvout |
| 203 |
můžeme zkontrolovat pomocí \message{\meaning\cnvout}. |
| 204 |
|
| 205 |
Jak probíhala konverze: |
| 206 |
|
| 207 |
\uv{\TeX} => \leva \TeX \prava |
| 208 |
\TeX => TeX |
| 209 |
TeX => TeAB |
| 210 |
TeAB => Te:XX:\TeX:B ... a toto už se nekonvertuje. |
| 211 |
|
| 212 |
Protože při \predef je možné deklarovat parametry makra, nesmí za |
| 213 |
deklarovaným tokenem být mezera (viz \predef X{AB} v ukázce). |
| 214 |
Na druhé straně při deklaraci pomocí \findef je možné mezi tokenem a |
| 215 |
tělem makra vložit nepovinnou mezeru. |
| 216 |
|
| 217 |
Při konverzi tokenu deklarovaného pomocí \predef provede \TeX{} |
| 218 |
expanzi tokenu jako by to bylo makro. Provede ale jen první úrovneň |
| 219 |
expanze (tj. jako při \expandafter, nikoli jako při \edef). |
| 220 |
|
| 221 |
Tokeny kategorie 10, 1 a 2 (mezera a svorky) se zpracovávají mírně |
| 222 |
odlišným způsobem, než ostatní tokeny. To znamená, že jsem trochu |
| 223 |
kecal, když jsem dříve ujistil čtenáře, že konverze je na kategoriích |
| 224 |
zcela nezávislá. Pokud je konverze mezery nedeklarovaná, pak se mezera |
| 225 |
nemění bez závislosti na hodnotě makra \cnvdefault. Konverzi mezery |
| 226 |
můžeme deklarovat například takto: |
| 227 |
|
| 228 |
\findef { } {<výstup pro mezeru>} |
| 229 |
nebo |
| 230 |
\predef { }{<makro pro mezeru>} |
| 231 |
|
| 232 |
Otevírací svorka, která ma kategorii 1, se interně promění v sekvenci |
| 233 |
\BGROUP a pokud není pro ní deklarována konverze, je zpracována pomocí |
| 234 |
\cnvdefault jako každý jiný nedeklarovaný token. Podobně zavírací |
| 235 |
svorka, která má kategorii 2, se promění v \EGROUP. Konverzi pro tyto |
| 236 |
tokeny můžeme deklarovat jednoduše například: |
| 237 |
|
| 238 |
\findef \BGROUP {<výstup pro otevírací svorku>} |
| 239 |
\findef \EGROUP {<výstup pro zavírací svorku>} |
| 240 |
nebo |
| 241 |
\predef \BGROUP {<makro pro otevírací svorku>} |
| 242 |
\predef \EGROUP {<makro pro zavírací svorku>} |
| 243 |
|
| 244 |
Bohužel, makro \cnvin není implicitně schopno rozlišit mezi skutečnou |
| 245 |
svorkou (kategorie 1 nebo 2) a zástupnou kontrolní sekvencí \bgroup, |
| 246 |
\egroup. Obé je interně převedeno na \BGROUP, \EGROUP a následně |
| 247 |
zpracováno. Chceme-li mezi svorkami a zástupnými kontrolními |
| 248 |
sekvencemi rozlišovat, musíme před konverzí změnit (aspoň přechodně) |
| 249 |
význam \bgroup, \egroup: |
| 250 |
|
| 251 |
{\let\bgroup=\relax \let\egroup=\relax \cnvin{...} ...} |
| 252 |
|
| 253 |
Pro možnost deklarace konverze akcentovaných znaků zapsaných |
| 254 |
v \TeX{}ové notaci je k dispozici ještě další deklarační makro: |
| 255 |
|
| 256 |
\cnvaccent <sekvence> <znak> {<výstup>}. |
| 257 |
|
| 258 |
Toto makro zařídí, aby se akcentované řídicí sekvence ve tvaru |
| 259 |
<sekvence> <znak> i ve tvaru <sekvence> {<znak>} konvertovaly na |
| 260 |
odpovídající <výstup>. Navíc se makro \cnvaccent při ukládání |
| 261 |
informací do konverzní tabulky pokusí expandovat "<sekvence> <znak>" a |
| 262 |
je-li výsledkem jediný <token>, provede ještě \findef <token> {<výstup>}. |
| 263 |
Tím je zaručeno, že na stejný <výstup> se konvertuje i akcentovaný |
| 264 |
znak, pokud je zapsán na vstupu "přímo" bez použití \TeX{}ové notace. |
| 265 |
|
| 266 |
Jakmile \cnvaccent deklaruje <sekvenci> jako sekvenci pro akcent, pak |
| 267 |
je sledován i výskyt neexistujícího akcentu vytvořeného touto |
| 268 |
<sekvencí>. Takový výskyt se konvertuje na obsah makra bez parametru |
| 269 |
\cnvadefault. Nechť například: |
| 270 |
|
| 271 |
\cnvaccent \v c {<výstup pro č>} |
| 272 |
|
| 273 |
a nechť chybí deklarace \v u. V takovém případě se \v c konvertuje na |
| 274 |
odpovídající výstyp zatímco \v u se konvertuje na obsah makra |
| 275 |
\cnvadefault. Implicitně je \def\cnvadefault{?}. |
| 276 |
|
| 277 |
Příkazy \predef, \findef a \cnvaccent interně definují výsledek |
| 278 |
konverze pomocí \gdef. Pokud chceme použít jinou metodu, můžeme před |
| 279 |
jejich použitím psát například \let\findefmethod=\xdef. |
| 280 |
Tím dosáhneme, že makra ve \findef se před zapamatováním expandují. |
| 281 |
Nebo třeba \let\predefmethod=\def způsobí, že deklarace \predef |
| 282 |
budou lokální vrámci skupiny. |
| 283 |
|
| 284 |
Existují tři příkazy, které mají při konverzi speciální význam: |
| 285 |
\cnvexec, \cnvexpandtext a \cnvexpand. |
| 286 |
|
| 287 |
Příkaz \cnvexec způsobí provedení následujících příkazů expand procesorem i |
| 288 |
hlavním procesorem \TeX{}u. Zbytek vstupu pro konverzi je připraven ve |
| 289 |
vstupní frontě. Konverze pokračuje až v okamžiku, kdy je |
| 290 |
zpracován příkaz \cnvnext. Následující token za tímto příkazem podléhá |
| 291 |
konverzi. |
| 292 |
|
| 293 |
Příkaz \cnvexpand <token> způsobí, že <token> bude expandován běžným |
| 294 |
způsobem do první úrovně expanze a výsledek této expanze bude dále |
| 295 |
konvertován. Příkaz je implementován jako \cnvexec\expandafter\cnvnext. |
| 296 |
|
| 297 |
Příkaz \cnvexpandtext {<text>} způsobí, že se <text> zcela expanduje běžným |
| 298 |
způsobem (jako při \edef) a pak se na takto expandovaný <text> provede |
| 299 |
konverze. |
| 300 |
|
| 301 |
Při načítání konverzní tabulky musí být obsah makra \cnvtable stejný |
| 302 |
jako při jejím použití v době konverze příkazem \cnvin. Změnou makra |
| 303 |
\cnvtable můžeme deklarovat více nezávislých tabulek a pak mezi nimi |
| 304 |
v době konverze přepínat. Implicitní hodnota makra \cnvtable je prázdné |
| 305 |
makro. |
| 306 |
|
| 307 |
|
| 308 |
PŘÍKLADY |
| 309 |
-------- |
| 310 |
|
| 311 |
V souboru cnv-pu.tex je deklarována konverzní tabulka, pomocí níž je |
| 312 |
možné konvertovat texty do PDF záložek v UNICODE. Výhodou tohoto |
| 313 |
řešení je, že nedochází k expanzi textu a že je možno nastavit pro |
| 314 |
konverzi jakýkoli znak, ačkoli tyto znaky nejsou aktivní. |
| 315 |
|
| 316 |
V rámci konverzí zdrojového textu TeXu můžeme narazit na problém |
| 317 |
obvyklé TeXové ligatury -- a ---, kterou bychom chtěli konvertovat na |
| 318 |
odpovídající výstup. Toto můžeme provést následujícím makrem: |
| 319 |
|
| 320 |
\predef -{\cnvexec \futurelet \nextchar \testtwodash} |
| 321 |
\def\testtwodash {\ifx-\nextchar \expandafter \twodash |
| 322 |
\else \expandafter \cnvnext \expandafter \onedash \fi} |
| 323 |
\def\twodash #1{\futurelet \nextchar \testthreedash} |
| 324 |
\def\testthreedash {\ifx-\nextchar \expandafter \threedash |
| 325 |
\else \expandafter \cnvnext \expandafter \twodash \fi} |
| 326 |
\def\threedash #1{\cnvnext \threedash} |
| 327 |
\findef\onedash {<simple minus>} |
| 328 |
\findef\twodash {<en-dash>} |
| 329 |
\findef\threedash {<em-dash>} |
| 330 |
|
| 331 |
Sofistikovanější příklad na konverzi celých úseků slov na jiný výstup |
| 332 |
(tj. nejen jednotlivých znaků) je uveden v souboru cnv-word.tex. |
| 333 |
Za použití maker z tohoto příkladu pak problém ligatur -- a --- |
| 334 |
lze deklarovat jednoduše: |
| 335 |
|
| 336 |
\stringdef {---} {<em-dash>} |
| 337 |
\stringdef {--} {<en-dash>} |
| 338 |
\stringdef {-} {<simple minus>} |
| 339 |
|
| 340 |
======================================================================= |
| 341 |
English documentation: |
| 342 |
|
| 343 |
|
| 344 |
After applying \cnvin{<list of tokens>} the converted list of tokens is |
| 345 |
saved in \cnvout macro. |
| 346 |
|
| 347 |
The conversion is done without the standard expansion of <list of tokens> |
| 348 |
and is independet of catcodes of converted tokens. |
| 349 |
The conversion process is done in two steps: |
| 350 |
|
| 351 |
1. Controlled partial expansion of tokens. The conversion process is |
| 352 |
returned back to the result of this expansion and converts it once |
| 353 |
more. Only tokens declared by \predef in the conversion table are |
| 354 |
expanded this way, other tokens not defined this way are not |
| 355 |
expanded (even if these tokens are expandable in the normal \TeX{} |
| 356 |
meaning). |
| 357 |
2. Final conversion of token: token is converted to the output string |
| 358 |
of tokens following to the declaration by \findef macro. The |
| 359 |
conversion process does not return to this output anymore. |
| 360 |
|
| 361 |
The tokens subject to conversion can have an arbitrary catcode. Not only control |
| 362 |
sequences can be converted. |
| 363 |
|
| 364 |
The declarations of tokens by \predef and \findef are quite |
| 365 |
independent of the normal meaning of these tokens. It means that in case of |
| 366 |
control sequences these sequences will behave during the usual processing |
| 367 |
by \TeX{} in a usual way although for the purpose of conversion their meaning |
| 368 |
can be changed using \predef or \findef. |
| 369 |
|
| 370 |
You can use normal parameters and separators (#1#2 etc.) when \predef |
| 371 |
is used. |
| 372 |
|
| 373 |
Each token can have only one meaning declared by \predef or \postdef. |
| 374 |
It the token is declared by boths declarators, then \predef is |
| 375 |
preferred. If the token is declared by the same declarator twice (or |
| 376 |
more times), then the last declaration is valid. |
| 377 |
|
| 378 |
If a token is to be converted which is not declared (by \predef nor |
| 379 |
\findef), then the conversion process does the default conversion |
| 380 |
which is determined by \cnvdefault macro (with one parameter: it is |
| 381 |
the coverted token). By default the token is simply copied to the |
| 382 |
oputput but you can redefine the \cnvdefault macro in the following |
| 383 |
way: |
| 384 |
|
| 385 |
\def\cnvdefault #1{<out>} % each undeclared token is converted to the |
| 386 |
% common <out> |
| 387 |
\def\cnvdefault #1{} % undeclared token is silently ignored |
| 388 |
|
| 389 |
Now, the simple hypothetic example follows: |
| 390 |
|
| 391 |
\predef \TeX {TeX} |
| 392 |
\predef \uv #1{\leva #1\prava} |
| 393 |
\predef X{AB} |
| 394 |
\findef A {:XX:\TeX:} |
| 395 |
\cnvin{This is \uv{\TeX}} |
| 396 |
|
| 397 |
The text "This is \leva Te:XX:\TeX:B\prava" is stored in \cnvout after |
| 398 |
\cnvin command is processed. The contents of \cnvout can be checked |
| 399 |
with \message{\meaning\cnvout}. |
| 400 |
|
| 401 |
The explanation of the conversion in this example: |
| 402 |
|
| 403 |
\uv{\TeX} => \leva \TeX \prava |
| 404 |
\TeX => TeX |
| 405 |
TeX => TeAB |
| 406 |
TeAB => Te:XX:\TeX:B ... and this is kept untouched. |
| 407 |
|
| 408 |
As parameters can be used with \predef declaration you must not |
| 409 |
write redundant space after the declared token (see \predef X{AB}). |
| 410 |
On the other hand, the optional space is allowed between the token |
| 411 |
and the body of the macro in \findef declaration. |
| 412 |
|
| 413 |
During conversion process \TeX{} treats the token declared with |
| 414 |
\predef as a real macro. Only first level of expansion is done before |
| 415 |
conversion process returns back to expanded material (similar to |
| 416 |
\expandafter primitive, not \edef). |
| 417 |
|
| 418 |
The tokens of catcode 10, 1 and 2 (space and braces) are processed in |
| 419 |
a slightly different way. It means that a small lie was told when the |
| 420 |
catcode independency of the conversion process mentioned above had |
| 421 |
been declared. If space conversion is not declared, then it is copied |
| 422 |
without conversion and without dependency on \cnvdefault macro. You |
| 423 |
can declare the conversion of the space e.g. as follows: |
| 424 |
|
| 425 |
\findef { } {<output for space>} |
| 426 |
or |
| 427 |
\predef { }{<macro for space>} |
| 428 |
|
| 429 |
Opening brace with catcode 1 is changed to a control sequence \BGROUP |
| 430 |
internally. If this control sequence is not declared, then it is |
| 431 |
processed by \cnvdefault as any other undeclared token. Similarly, |
| 432 |
closing brace with catcode 2 is changed by \EGROUP during conversion |
| 433 |
process. We can declare the conversion rules for these tokens by: |
| 434 |
|
| 435 |
\findef \BGROUP {<output for open brace>} |
| 436 |
\findef \EGROUP {<output for close brace>} |
| 437 |
or |
| 438 |
\predef \BGROUP {<macro for open brace>} |
| 439 |
\predef \EGROUP {<makcro for close brace>} |
| 440 |
|
| 441 |
Unfortunately, macro \cnvin cannot implicitely distinguish between a |
| 442 |
real brace of catcode 1 or 2 and an alternate control sequence \bgroup |
| 443 |
or \egroup. Both are converted internally to \BGROUP, \EGROUP and |
| 444 |
processed in the same way. If you need to distinguish between them, |
| 445 |
then you need to set (locally) another meaning to \bgroup, \egroup |
| 446 |
before \cnvin is invoked: |
| 447 |
|
| 448 |
{\let\bgroup=\relax \let\egroup=\relax \cnvin{...} ...} |
| 449 |
|
| 450 |
You can declare the conversion of accented letters written by standard |
| 451 |
\TeX{} sequences. To do this, you can use the declaration macro: |
| 452 |
|
| 453 |
\cnvaccent <sequence> <char> {<output>}. |
| 454 |
|
| 455 |
This macro arranges that the accented control sequences in the form |
| 456 |
<sequence> <char> or <sequence> {<char>} will be converted to |
| 457 |
<output>. Moreover, when storing an information to a conversion table |
| 458 |
the \cnvaccent macro tries to expand the "<sequence> <char>" by normal |
| 459 |
\TeX{} rules. If the output of this expansion is one <token>, then |
| 460 |
\cnvaccent does internally more work: \findef <token> {<output>}. |
| 461 |
This guarantees that even an accented character will be converted |
| 462 |
to the same <output> if it is written in the input "directly" without |
| 463 |
\TeX{} notation. |
| 464 |
|
| 465 |
When \cnvaccent declares <sequence> as a sequnce for an accent, then the |
| 466 |
occurence of a non-existing accent created with this <sequence> is watched. |
| 467 |
Such an occurence is converted to the contents of the macro \cnvadefault |
| 468 |
without parameters. |
| 469 |
For example: |
| 470 |
|
| 471 |
\cnvaccent \v c {<output for ccaron>} |
| 472 |
|
| 473 |
and suppose that \v u is not declared. In such a case the \v c is |
| 474 |
converted to the corresponding output, but \v u is converted to the contents of |
| 475 |
\cnvadefault macro. By default there is \def\cnvadefault{?}. |
| 476 |
|
| 477 |
The commands \predef, \findef and \cnvaccent define internally the result |
| 478 |
of the conversion using \gdef primitive If we want to use another method, |
| 479 |
we can put e.g. \let\findefmethod=\xdef before the use of macros. |
| 480 |
This causes that all bodies of \predef will be expanded before saving them |
| 481 |
to the conversion table. Similarly \let\predefmethod=\def results in \predef |
| 482 |
will work only locally. |
| 483 |
|
| 484 |
There are three commands with special meaning in conversion process: |
| 485 |
\cnvexec, \cnvexpandtext and \cnvexpand. |
| 486 |
|
| 487 |
The \cnvexec command causes the following command is processed both by |
| 488 |
\TeX{} expand processor and main processor. |
| 489 |
The rest of an unconverted input is ready in the input stream. |
| 490 |
The conversion process continues at the moment the \cnvnext command is processed. |
| 491 |
The following token behind this command is submitted to conversion. |
| 492 |
|
| 493 |
The \cnvexpand <token> command expands <token> at first level of |
| 494 |
expansion and the expanded result is converted again. The \cnvexpand |
| 495 |
is implemented by \cnvexec\expandafter\cnvnext. |
| 496 |
|
| 497 |
The \cnvexpandtext {<text>} expands <text> totally in a usual way |
| 498 |
(like by \edef) and the result of this expansion is converted again. |
| 499 |
|
| 500 |
The contents of \cnvtable macro has to be the same both during the declaration |
| 501 |
of the conversion table (by \predef, etc.) and during the conversion process |
| 502 |
itself (by \cnvin). You can change the contents of \cnvtable macro if |
| 503 |
you need to declare more independent conversion tables. Then you can |
| 504 |
switch among these tables before using \cnvin command. |
| 505 |
By default there is \def\cnvtable{}. |
| 506 |
|
| 507 |
|
| 508 |
EXAMPLES |
| 509 |
-------- |
| 510 |
|
| 511 |
There is an example in cnv-pu.tex file. The conversion table which |
| 512 |
declares the conversion from TeX text to UNICODE text used in PDF |
| 513 |
outlines is declared here. The advantage of this solution is that we |
| 514 |
need not to expand the converted text and we can declare the |
| 515 |
conversion for each character, not only for active characters. |
| 516 |
|
| 517 |
There is a common problem with the conversion of TeX ligatures -- and |
| 518 |
--- to the desired output. This task can be solved by the following |
| 519 |
macro: |
| 520 |
|
| 521 |
\predef -{\cnvexec \futurelet \nextchar \testtwodash} |
| 522 |
\def\testtwodash {\ifx-\nextchar \expandafter \twodash |
| 523 |
\else \expandafter \cnvnext \expandafter \onedash \fi} |
| 524 |
\def\twodash #1{\futurelet \nextchar \testthreedash} |
| 525 |
\def\testthreedash {\ifx-\nextchar \expandafter \threedash |
| 526 |
\else \expandafter \cnvnext \expandafter \twodash \fi} |
| 527 |
\def\threedash #1{\cnvnext \threedash} |
| 528 |
\findef\onedash {<simple minus>} |
| 529 |
\findef\twodash {<en-dash>} |
| 530 |
\findef\threedash {<em-dash>} |
| 531 |
|
| 532 |
A more sophisticated example of word to word conversion (not only |
| 533 |
single letters) is presented in cnv-word.tex file. If you use macros |
| 534 |
from this example, then you can solve the problem of -- and --- |
| 535 |
ligatures simply: |
| 536 |
|
| 537 |
\stringdef {---} {<em-dash>} |
| 538 |
\stringdef {--} {<en-dash>} |
| 539 |
\stringdef {-} {<simple minus>} |
| 540 |
|
| 541 |
======================================================= |