# Can openout handle a filename with spaces?

With pdflatex, can \openout handle a filename with a space in it? (We're using MiKTeX and our users seem to think it's clunky to restrict them in this way. We also have tons of legacy TeX files and would prefer to continue using pdftex/pdflatex.)

Background: The Output filename is based on the input jobname - we handle the space/asterisk issue using the answer here : Why does \jobname give asterisks/stars instead of spaces, and how do I fix this?

We simply append a new Extension for our usage.

In some constellations (which are rare, but not unthinkable if one bothers to think about them, we get a space in the middle of the filename. Then pdflatex returns the message:

! Missing \$ inserted.

and stops.

We've got some rigid filenaming conventions, so replacing the space with an underscore (or another character) isn't doable in this case.

MWE: Openout is used in a package as follows:

   \myStuff@filename <- gets a filename based on the -jobname. See link above.
Unfortunately it could be    "foo bar" not "foobar"

\immediate\openout\myStuff@openout=\myStuff@filename

• Quotes should work \immediate\openout\myStuff@openout="\myStuff@filename" – Ulrike Fischer Feb 5 '15 at 16:23
• @UlrikeFischer web2c based tex should make \jobname usable already, adding the " automatically, doesn't miktex do that? in texlive pdftex a\ b inputs a b.tex and \edef\x{\jobname}\show\x produces > \x=macro: ->"a b". – David Carlisle Feb 5 '15 at 16:30
• @DavidCarlisle: I have no idea how \myStuff@filename is based on \jobname. I tried with a simple command with the definition foo bar.txt. But yes: There are quotes around \jobname and so  \immediate\openout\testout=\jobname.txt works out of the box. (What should one do if one want \jobname-blub.txt?) – Ulrike Fischer Feb 5 '15 at 16:41
• @UlrikeFischer let's wait for a MWE:-) – David Carlisle Feb 5 '15 at 16:43
• Some testing suggests MiKTeX is happy both with the 'raw' \jobname and one where the star-to-space change has been made: I get the correct file name written in all cases tested. We'll therefore need a complete example and ideally log extract to help further. (MiKTeX version may also be relevant: I'm testing with 2.9, but note on your profile you have some legacy issues.) – Joseph Wright Feb 5 '15 at 18:47

## 1 Answer

Y'all are great! I'm sorry about not posting how I derive myStuff@filename. Should I re-edit the original question? We're using either MikTeX 2.9 or 2.3 (trying to phase this out)

Not using quotes was the problem. \Verbdef from newverbs also works.

Here's an example of the bad code:

% The following actually writes to 'my.tex'
\renewcommand\TestFilename{my test outputB.file}
\newwrite\MyFilehandle
\immediate\openout\MyFilehandle=\TestFilename
. . .


Ulrikes solution works great - click there, but everyone deserves a up-click

Solution code - with jobname etc.

% Variations in run command to illustrate general filename requirements ...
% h:\MikTeX\MikTeX.2.9p\texmf\miktex\bin\pdflatex -job-name=OpenOutTestJobname "OpenOutTestJobname.tex"
% h:\MikTeX\MikTeX.2.9p\texmf\miktex\bin\pdflatex "-job-name=Open Out Test Jobname" "OpenOutTestJobname.tex"
% This is the stupid requirement - need to handle more than one contiguous space!
% h:\MikTeX\MikTeX.2.9p\texmf\miktex\bin\pdflatex "-job-name=Open  Out  Test  Jobname" "OpenOutTestJobname.tex"
% Test writing to openout - attempt reading in the same file into a pdfobj
\pdfoutput=1
\documentclass[german]{article}
\usepackage[a4paper]{geometry}
\begin{document}
\typeout{jobname=\jobname}
%%%
%%% Convert jobname
\edef\JobnameX{\jobname}
\catcode\*=\active
\def*{ }
\edef\JobnameX{\scantokens\expandafter{\JobnameX\noexpand}}
\catcode\*=12 %
\newcommand{\TestFilename}{\JobnameX .ips}
\typeout{TestFilename=\TestFilename}
%%%
%%% Write stuff to output - then read it back into a pdfobj
\newwrite\MyFilehandle
\immediate\openout\MyFilehandle="\TestFilename"
\typeout{Test writing stuff to output file=\TestFilename}
\immediate\write\MyFilehandle{Test writing stuff to output file=\TestFilename}
\immediate\closeout\MyFilehandle
\immediate\pdfobj file {\TestFilename} %%% Just to show a snapshot of the entire process.
%%%   FYI - we embed papertray/bursting/external page requirement commands for postprocessing
%%%   (actually written as a 'pagepiece' dictionary)
%%%
%%% Write some text to create a pdf
Write some text into the pdf.
\end{document}
\endinput


Console transcript snippet (for second run command variation):

. . .
jobname=Open*Out*Test*Jobname
TestFilename=Open Out Test Jobname.ips
Test writing stuff to output file=Open Out Test Jobname.ips
<<Open Out Test Jobname.ips>>
. . .


Resulting PDF snippet:

. . .
1 0 obj
Test writing stuff to output file=Open Out Test Jobname.ips
endobj
. . .