Passing Options to Unknown Class

My goal is to process a LaTeX tex file with the draft option enabled for the document class, but from a terminal without having to edit the tex file or even open it to look at what class is being used, given that I know it's a class which has the draft option. The option pdflatex -draftmode isn't what I'm looking for since it doesn't produce a pdf.

For example, suppose main.tex contains the following:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
\includegraphics{myfig}
\end{document}


Using \documentclass[draft]{article} should of course produce a box with the text "myfig.xxx" in it. The solution given to the question Add option to class with command would almost be enough. I would use

pdflatex \PassOptionsToClass{draft}{article} \input{main}


But this isn't satisfactory since I have to know that the class is article rather than book or anything else.

As perhaps a second issue that might affect an answer; this solution doesn't even seem to entirely work given article, since it produces the image normally and not the box with its title inside. This surprises me since passing the option 12pt in this manner seems to work properly. But I guess the option doesn't get passed through to graphicx or globally to other packages this way.

-
This might be something you want to do with a perl or sed script. Would that meet your needs? – A.Ellett Sep 26 '13 at 18:45
I'd welcome any working answer. A TeX only solution is ideal of course, but I'm not too picky. I run the commands through a Windows batch file. – Marcus Fontaine Sep 26 '13 at 18:50

Document classes execute a number of "default" options via \ExecuteOptions. For example, article.cls calls

\ExecuteOptions{letterpaper,10pt,oneside,onecolumn,final}


while book.cls calls

\ExecuteOptions{letterpaper,10pt,twoside,onecolumn,final,openright}


You could override and always pass draft to \ExecuteOptions using the following command-line "interface":

pdflatex \let\oldExecuteOptions\ExecuteOptions \def\ExecuteOptions#1{\oldExecuteOptions{#1,draft}} \input{main}


The above stores \ExecuteOptions in \oldExecuteOptions before redefining it to always end with the draft option.

-
Another nice thing about this is that it doesn't complain if draft is already specified with the document class in main.tex. \ExecuteOptions seems to have no problems with draft being there more than once. – Marcus Fontaine Sep 26 '13 at 19:51
@MarcusFontaine: Yes, however useless, there's no problem specifying \documentclass[draft,draft,draft,draft,draft,draft]{article} if you so wish. :) – Werner Sep 26 '13 at 22:19

Here's a perl script which will make a copy of your main.tex file and create a formatted_main.tex file which you can LaTeX. It finds the \documentclass line and rewrites it:

#!/usr/bin/perl
my ($filehandle) = @ARGV; open FILEHANDLE, "<$filehandle" or die "cannot open < $filehandle:$!";
my @file = <FILEHANDLE>;
close FILEHANDLE;

foreach my $line ( @file ) { if ($line =~ /^\s*\\documentclass$/ ) { line =~ s/(\\documentclass)(\[)/12draft,/; } if ( line =~ /^\s*\\documentclass{/ ) { line =~ s/(\\documentclass)({)/1\[draft$$2/; } } open FILE, ">formatted_$filehandle";
foreach my $line ( @file ){ print FILE$line;
}
close FILE;


UPDATE

One problem with this script is that it expects that whoever wrote main.tex did not split the \documentclass macro from its arguments. In other words, this script will not help you if main.tex contains a line like

\documentclass
[12pt]{article}


If you're concerned that this might be a possibility, then I would definitely go with @Werner 's solution.

-
The tex files I work with can have odd things going on with them, like you've keenly pointed out. Thank you for making this answer though. – Marcus Fontaine Sep 26 '13 at 19:29