On 02-Jan-12 12:46 am, Richard Heck wrote:
On 01/01/2012 06:46 PM, Bogdan wrote:
On 01-Jan-12 10:00 pm, Richard Heck wrote:
On 01/01/2012 04:27 PM, Bogdan wrote:

pdflatex "\def\myvar{true}\input{myfile.tex}"

One way to do this, then, would be (a) to define a new file format, call
it pdfx (more or less copy over the definition of the PDF (pdflatex)
format, but making that one change from pdf2 to pdfx) and then define a
custom converter for latex --> pdfx. The one for latex --> pdf2 is:
pdflatex $$i
so you'd have to get the quotes right, etc, to make that work.

Not ideal, I know. But if we fix this, it will be 2.1 before the fix is
out, since it will definitely change the file format (probably of layout
files).

Thanks for replying Richard. I already defined a new converter like
the above. I use it to compile two versions of the same problem sheet,
one with answers, one without (there is a bug with defining that
converter for which I found a workaround but I'll make a separate post
about it).

However, as you say, it's quite far from ideal for the purpose of
paper margins since I'd have to keep modifying the converter and/or
file format to fit each document margin requirements etc. Gets the job
done for now but it would quite nice to have the possibility of adding
code before calling \documentclass{} in the future. Shall I file an
enhancement request about this, pointing to this discussion?

Sure.

I hacked a nice workaround for this that works transparently and safely in all cases. I created a converter wrapper (works for any latex converter) which replaces the \documentclass[...]{...} after searching for the string "predocclass=<something>" inside [...] and then prepends that to \documentclass{}. This is safe, doesn't break anything as far as I've seen and works on the fly whenever the converter is invoked (update, view, export etc).

I uploaded the windows package at http://db.tt/5jfLpZh4 (unzip it into $LyXDir\bin). I provide the code and explanations below as well. The *nix folks would find it trivial to port since I'm using sed in windows anyway.

In LyX I modify the document class options in Document > Options > Document Class > Class options to read for example:

a4paper,nofonttune,predocclass=\newcommand{\CLASSINPUTbottomtextmargin}{2cm}

The windows shell script lyxwrap.bat which I placed in $LyXDir\bin has the following contents (you can download sed for windows from the unxutils or gnuwin32 projects on sourceforge):

@echo off
for %%a in (%*) do set last=%%~a
sed -i -r -e "s/^(\\documentclass\[.*?)predocclass=([^],]+)/\2\n\1/" "%last%"
%*

In LyX, I change the Latex (pdflatex) -> PDF (pdflatex) converter from:

pdflatex --synctex=-1 $$i

into:

lyxwrap pdflatex --synctex=-1 $$i

All *tex ones can be changed, including those that do not have placeholders like $$i, e.g. from "pdflatex" into "lyxwrap pdflatex", from "xelatex" into "lyxwrap xelatex", etc.

Whenever the converter is called (view, update, export, etc), the wrapper will parse and replace the \documentclass[]{} definition accordingly. In my example above, LyX would output the following tex code:

%% LyX 2.0.2 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[12pt,british,a4paper,nofonttune,\newcommand{\CLASSINPUTbottomtextmargin}{2cm}]{IEEEtran}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
...

and the lyxwrap script replaces it on the fly with

%% LyX 2.0.2 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\newcommand{\CLASSINPUTbottomtextmargin}{2cm}
\documentclass[12pt,british,a4paper,nofonttune,]{IEEEtran}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
...

The dangling "," hasn't been a problem until now and should't be. I couldn't get rid of it with sed since sed can't do non-greedy matching. If you have Perl installed then use that since the PCREs are better. There is an issue if your predocclass=<...> contains a comma "," but you can fix that if you need to by encoding it into "{COMMA}" and replacing it back into "," inside lyxwrap (I couldn't be bothered that far).

I think the devs could add an extra text field next to "Class options" (called, say, Pre documentclass code) that would be parsed in the same way that my hack does, which doesn't break anything and would not need any alteration of the LyX layout format or anything, it's perfectly backwards-compatible.

Hopefully this is useful for others too.
Bogdan.

Reply via email to