[OS X TeX] Re: [XeTeX] documentation on programming language (in macros)?

Bruno Voisin bvoisin at mac.com
Sat Feb 26 03:23:32 EST 2005

Le 26 févr. 05, à 06:31, Roger Hart a écrit :

> I certainly very much appreciate all the extremely generous and expert 
> help that I've received with my many very ignorant and simple novice 
> questions about latex etc.  So please let me apologize for asking one 
> more, and I do hope I may forgiven for stating this, since I really 
> don't know anything about programming in TeX and LaTeX at this point, 
> but it seems to me that making ps4pdf work with xelatex should be 
> pretty trivial exercise:

I don't think it's trivial at all. If you look at the code inside 
ps4pdf.sty, you'll see it defines first a switch \PfP at mode, initialized 
to 0 then set to 1 if you're running pdfTeX and 9 if you're running 
VTeX. in ps4pdf's approach, the initialization corresponds to the case 
when you're running standard TeX (producing a PS file to be later 
processed by dvips then ps2pdf to create the required PDF graphics 
container, a multi-page PDF file with one graphics per page).

The recognition of pdfTeX and VTeX is done via Heiko Oberdiek's packages


Basically, ifpdf.sty defines the conditional \ipdf, checks whether the 
command \pdfoutput is defined, and, if so, sets \ifpdf to true; 
otherwise it sets it to false. Similarly, ifvtex.sty defines \ifvtex, 
checks whether \VTeXversion is defined and, if so, sets \ifvtex to true 
and otherwise to false. Creating a similar ifxtex.sty for XeTeX 
wouldn't be difficult, by checking whether \XeTeXversion is defined; 
see e.g.


But that's only the beginning! Then comes the actual work ps4pdf.sty is 

- First comes the job done when you're running normal TeX, 
corresponding to \PfP at mode=0 and starting with

	\ifcase\PfP at mode%      == dvi (extraction mode) ==

- Next comes the job done when pdfTeX is run, corresponding to 
\PfP at mode=1 and starting with

	\or%                   == pdfTeX ==

- Finally comes the job done in other cases, corresponding to other 
values of \PfP at mode and starting with

	\else%                == inactive (latex/dvips, VTeX) ==

So what you'd have to do is first add a new possible value, say 2, of 
\PfP at mode, corresponding to XeTeX, by changing

	\ifnum\PfP at mode>0\relax\else
	    \def\PfP at mode{9}
	      \def\PfP at mode{1}


	\ifnum\PfP at mode>0\relax\else
	    \def\PfP at mode{9}
	      \def\PfP at mode{2}
	        \def\PfP at mode{1}

Pretty simple I agree. But then you'd need to do the hard work: rewrite 
the pdfTeX-specific code in such a way that it works with XeTeX. That 
is, adapt all the code in ps4pdf.sty between

	\or%                   == pdfTeX ==


	\else%                == inactive (latex/dvips, VTeX) ==

Not for the faint of heart!

ps4pdf.sty seems to be very clever in that it relies, apparently, on 
functionalities and commands from the graphicx package to do most of 
its work, being then as driver-agnostic (driver = dvips, pdfTeX, XeTeX, 
etc.) as possible. However, it relies still on some driver-specific 
commands, such as \pdflastximagepages specific to pdfTeX. In any case, 
you'd have to be an expert, knowing the inner workings of the graphicx 
package well, to do the required rewriting.

This is made all the more difficult as ps4pdf.sty is very sparsely 
documented, with a few comments. On CTAN, there is no accompanying 
documented code ps4pdf.dtx telling exactly what's done and how.

I've tried running the test file ps4pdf-test.tex with TeX -> dvips -> 
ps2pdf -> XeTeX, forcing either \PfP at mode to 1 or 9 (there's an 
[inactive] package option for the latter), with no luck: in both cases, 
when processing the included file 4-10-8.inl, an error occurs when 
reaching \pscircle:

	) (./4-10-8.inl
	! Undefined control sequence.
	<recently read> \c at lor@to at ps

	<to be read again>
	<argument> 0
	\xcolor@ #1#2#3#4->#2
	\XC@@usecolor ...er \expandafter \c at lor@to at ps #1#2
	                                                  \@@ \else 
\expandafter \ex...

	\addto at pscode #1->\xdef \pst at code {\pst at code #1
	                                               \space }
	\psframe at ii (#1)(#2)->\begin at ClosedObj
	                                       \pst at getcoor {#1}\pst at tempa 
	<to be read again>
	l.5   \pscircle
	               [fillcolor=yellow](2,6){.8} % Sun

I think the only hope here is getting an expert in the LaTeX graphics 
package, and who also knows XeTeX well, interested enough to do the 
work; or contact the author of ps4pdf.sty and get him interested as 

There's also a script ps4pdf, by Thomas Esser, inside 
/Library/teTeX/bin/powerpc-apple-darwin-current/. I've not tried to run 

> 1)  add a section of code that tests to see if the environment is 
> xelatex, and if it is, use \XeTeXpdffile, instead of the corresponding 
> pdflatex command, to insert the contents of myfilename-pics, one page 
> at a time;
> 2) if the environment is not xelatex, then process according to 
> ps4pdf, either creating a -pics file under latex + dvips +ps2pdf, and 
> even allowing the usual processing under pdflatex.
> That is, something like,
> case of xelatex {
> %set definitions
>      \newcounter{pdfpage}
>      \setcounter{pdfpage}{1}
> %redefine ps4pdf for xelatex
> \renewcommand{ps4pdf}{
>      \XeTeXpdffile "scratch-xy-pics.pdf" page pdfpage
>      \stepcounter{pdfpage}
>      }
> }
> case of latex + dvips +ps2pdf {
>      process what is already in ps4pdf
> }
> case of pdftex{
>      process what is already in ps4pdf
> }
> Or even,
> if xelatex {
> insert pages as above
> }
> else
> \usepackage{ps4pdf}
> endif

See above. The problem is "low"-level commands such as \XeTeXpdffile 
are not run directly, the ps4pdf.sty code is much more obscure than 

> Obviously, as you can see from the above, I know nothing about the 
> syntax or commands of macros.  But I'm growing frustrated enough with 
> macros and other things that don't work with CJK that it seems about 
> time I learn.  So my question is, where is all of this language 
> documented? I tried imitating some .sty files just to find a test for 
> xetex, but I was unable to get any of the conditional loops to do what 
> I want. I have all of the LaTeX books, and none of them document 
> commands like \if ... \fi, \group ... \endgroup, etc. that are used in 
> macro commands.I didn't see any documentation on the web.
> Is this TeX, documented in Knuth's TeXbook? If so, I'll purchase it 
> right away. It seems like I would learn a lot by completing this 
> exercise, even if it is indeed as trivial as I assume.
> Please let me know what the best and most comprehensive source is so I 
> can begin to write code when necessary.

The TeXbook is the definitive source of information on TeX. Be warned: 
it's not easy to learn: IMHO it's written more with quality of writing 
(= quality of prose) than efficiency in mind. Everything's there, and 
enjoyable to read, but generally it's difficult to find out where to 
look for a specific piece of information. You need to make heavy use of 
the index, and generally you need to have first read the book once, 
without understanding much, and then upon successive re-readings you 
manage to find your way through it.

Alternative sources of information:

- TeX by Topic (donationware) <http://www.eijkhout.net/tbt/>: very 
thorough and technical, the same information as in the TeXbook but 
presented more like a software manual.

- A Gentle Introduction to TeX (free) 
<http://www.ctan.org/tex-archive/info/gentle/gentle.pdf>: plain TeX for 
the beginners.

- TeX for the Impatient (GPL) at 
/Library/teTeX/share/texmf.tetex/doc/plain/impatient/impatient.dvi and 
also <http://www.tug.org/ftp/tex/impatient/> (there's a French 
translation too at this site): another technical introduction to the 
inner workings of plain TeX, but more oriented towards a specific 
extension called Eplain (plain TeX + some functions of LaTeX like 
counters and cross-referencing - LaTeX taking over the presentation of 
your document), written specifically for this book.

The problem is all these sources only document plain TeX, the original 
TeX format, and none of the functionalities added by LaTeX and the 
various LaTeX packages like graphicx. TeX itself knows nothing about 
graphics, multimedia contents, etc., it was written before these things 
came into widespread use.

For these packages, the only source of information is the documented 
code, in the form of accompanying .dtx files, not included inside gwTeX 
but available from CTAN or the TeX-Live CDs. Sometimes, too, these 
files don't even exist; in that case the only solution is look at the 
code inside the .sty files, and try to figure out what it does.

You may also try the LaTeX Companion 2nd edition (I've not got it yet), 
it might tell more on these things.

Enough for today!

Bruno Voisin
--------------------- Info ---------------------
Mac-TeX Website: http://www.esm.psu.edu/mac-tex/
           & FAQ: http://latex.yauh.de/faq/
TeX FAQ: http://www.tex.ac.uk/faq
List Post: <mailto:MacOSX-TeX at email.esm.psu.edu>

More information about the MacOSX-TeX mailing list