Fonts & Character SetsForeign characters and \write

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
Post Reply
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Foreign characters and \write

Post by woj-k »

Hello everyone,

I am having trouble with the following piece of code.

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}

\newwrite\writekey
\immediate\openout\writekey=\jobname.key

\newcommand{\gap}[1]{
  \def\temp{#1}
  \immediate\write\writekey{\string\keyitem{\temp}}
  \temp
}

\newcommand{\keyitem}[1]{
  \def\temp{#1}
  \temp
}

\begin{document}
\gap{Za\.z\'o\l{}\'c g\k{e}\'sl\k{a} ja\'z\'n}

\immediate\closeout\writekey
\input{\jobname.key}
\end{document}
\gap is meant to print its argument, and also write it to an external file, as an argument of a \keyitem command, which is then invoked to print the argument again. That it does, but in the .key file created after running the above code through LaTeX I found that the argument had been written as a string of unrecognizable characters, with an extra \T1 before the \l{}, and some unnecessary spaces. The \T generates an "undefined control sequence" until I explicitly write \def\T1{\relax} in the preamble (ugly, I know).

Incredibly, the gibberish from the .key file is printed correctly in the .dvi file. However, I would like to keep all my files "pure" ASCII for the publisher's benefit, to preclude printing errors.

Can anyone here suggest a way to make LaTeX export to the .key file exactly what I give \gap in the .tex file? In other words, to make the .key file read

Code: Select all

\keyitem{Za\.z\'o\l{}\'c g\k{e}\'sl\k{a} ja\'z\'n}
after running the code? I would be extremely grateful for any hints.

I use Windows and MIKTeX 2.8.

Thank you for your time!

woj-k
Last edited by woj-k on Sun Nov 28, 2010 7:22 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Re: Foreign characters and \write

Post by localghost »

That's presumably the same issue as with command definitions and labels. TeX simply doesn't accept any special characters.


Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Re: Foreign characters and \write

Post by woj-k »

localghost, thank you for your reply.

I understand what you are saying, but in the string I input for the .key file there aren't any non-standard characters -- only backslashes, apostrophes, braces, letters. Or at least I'd like TeX to treat them as such, which for some reason it refuses to do, regardless how many \string commands I put in -- it continues to treat "\k{a}" as a single non-ASCII character, rather than five standard characters.

Additionally, if I change the input encoding appropriately using the inputenc package, LaTeX writes the .key file with the correct national characters and diacritics. In so doing, however, I would make the .key file non-ASCII, which I am trying to avoid.

I'm playing with redefining the character categories just for the purpose of \write, so it would stop treating \ as an escape character and \' or \l as control sequences, but no luck so far...
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Foreign characters and \write

Post by localghost »

I'm not an expert because I still have difficulties in understanding TeX. Especially macro expansion is a closed book to me. But I think that the argument for the \gap command expands first thus produces accented characters and causes trouble.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Foreign characters and \write

Post by woj-k »

Solved! (for the most part)

This is not nearly as cool as verbatim \write to a file would be, but it works in this particular case. I took advantage of only dealing with a finite set of control sequences, and I redefined each of them locally for the purpose of the \write command.

Code: Select all

\def\redefdiacritics{
  \let\'{\noexpand\'}
  \let\.{\noexpand\.}
  \let\l{\noexpand\l}
  \let\L{\noexpand\L}
  \let\k{\noexpand\k}
}
The definition of \gap now looks as follows.

Code: Select all

\newcommand{\gap}[1]{
\def\temp{#1}
{
  \redefdiacritics
  \immediate\write\writekey{\string\keyitem{\temp}}
}
\temp
}
This code allows the foreign characters, e.g. \.z, to be output output to the .key file as "\.z" exactly (three characters), not as a single character (the letter "z" with a superior dot). There are still some extra spaces present in the .key file, but these are absorbed by the control sequences. I don't know how or if this solution could be adapted to work on any control sequence used as the argument of \write.

Thank you everyone for any time and effort that you have devoted to helping me out with this problem!

Kind regards,
Wojtek
Post Reply