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.

Consider this short LaTeX document:

\documentclass[11pt]{article}

\usepackage[T2A]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}

\def\name{...}

\begin{document}
Hello: \name
\end{document}

The \def\name line will be generated by a program and the ... will contain TeX required to represent a string literal whose value comes from, say a database. Think of the classic mail-merge scenario.

For example, if the data value is Foo, then the program can generate: \def\name{Foo}, but if the data value is $x+y$, the program will need to output something like \def\name{\$x+y\$} in order that $ will not be interpreted as the switch to math mode markup command.

My question is: given a sequence of Unicode code-points, how can I algorithmically create a \def or \newcommand statement which when expanded will always result in that literal sequence of Unicode characters without any possibility of any characters being interpreted as markup command.

I imagine the procedure might depend on which packages are in use, so you can assume that the above packages are in effect - although it would be nice to have a procedure which works independently of context.

share|improve this question
    
You want to detokenize the stored information, by the sound of things. Are you printing stuff in a document or writing it to a file? (I'm thinking that it's easier to alter the output, rather than the input.) –  Joseph Wright Mar 25 '13 at 21:47
    
The "program" I am writing will create the LaTeX document (i.e. create a file), but, of course, the only part of the generated document which will change from run to run will be the \def line. Note that the program doesn't need to read a LaTeX document - it just takes a Unicode string as input and writes (say on stdout) a LaTeX document. –  user5402 Mar 25 '13 at 21:53
1  
If you only want to print stuff out, I'd just go with \detokenize\expandafter{\name}. The only proviso there will be to make sure you escape any % characters, as they will mess up the \def in the first place. The alternative is I guess to set up your \def in a verbatim-like group, but that sounds rather tiresome. –  Joseph Wright Mar 25 '13 at 22:01
add comment

1 Answer

Since you are using (pdf)tex and utf8 input, its tricky to use the usual \detokenize or \meaning to avoid special characters as you need to preserve the characters with the 8th bit set as active so that the decoding from utf-8 works.

As you are generating the document you really don't need many special characters $, & and % can all be made globally catcode 12 (normal punctuation). You can use \( \) for math, avoid comments and use \newcell defined via \let\newcell& to separate table cells.

You need an escape character, if there is any character that you know does not occur (eg U+0001 hasn't got much right to appear in a normal document) then you can give that character catcode 0 and use it instead of \

I use a scheme like this for the generated TeX source of the pdf version MathML specification.

share|improve this answer
add comment

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.