You are not logged in.
Hi,
I want to have a latex file with in it some ifdef's and stuff, so I can have different versions for different use cases.
I thought I would just use the C processor, I found this interesting tutorial for using the C preprocessor on javascript: http://web.archive.org/web/200801160344 … .com/?p=12
Where they recommend to use:
PREPROCESS_CORE=gcc -E -P -C -w -x c
Unfortunately this doesn't work very well. gcc doesn't know tex/latex, and when you set the language to c, it mangles up your latex.
For example, it changes:
\\
\headerrow
{\begin{tabular*}{\linewidth}{l@{\extracolsep{\fill}}r} #1 & #2 \\
\end{tabular*}}
into:
\ \headerrow
{\begin{tabular*}{\linewidth}{l@{\extracolsep{\fill}}r} #1 & #2 \\end{tabular*}}
which is not very fun. Basically it does "escaped newline splicing". In man gcc this feature is documented but I couldn't find a way to turn this off
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
Could you use awk?
Just a quick description:
*) default print $0
*) pattern for each line starting with #
*) #XXXX=YYYY set an associative array
*) #if then look-up the item in the array and check the value
*) print $0 depending on value
*) #endif revert to default
Would need a slight tweak for else.
There might even be one on the web somewhere.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
It's hard to know exactly what your needs are, but I would think LaTeX by itself provides you with all you need. By a combination of, e.g., its ifthen package, and/or including, e.g., the core in a separate file which makes use of commands which are defined in a wrapper file that calls the core with the \input command, so that the same command could be given different definitions by different wrappers, etc., so that running latex on one yields different output even though the core is unchanged.... and so on.
Offline
What frabjous says makes a lot of sense, but I'd already knocked this up (apologies for formatting):
1003 ~ $ cat scripts/preproc
#!/bin/bash
awk -F = '
BEGIN { out = 1 }
/^#if.*=.*$/ { adef = substr($1, 5)
if (adef in defs)
{
if (defs[adef] == $2)
out = 1
else
out = 0
}
}
/^#[^if].*=.*$/ { defs[substr($1, 2, 2)] = $2 }
/^#endif/ { out = 1 }
/^[^#].*$/ { if (out == 1) print $0 }
' $1
1004 ~ $cat testfile
#t1=in
this is line one
this is line two
this is line three
#if t1=in
XXXXXXXXXXX
#endif
this is line four
#if t1=out
YYYYYYYYYYY
#endif
this is line five
1005 ~ $preproc testfile
this is line one
this is line two
this is line three
XXXXXXXXXXX
this is line four
this is line five
Needs work- doesn't use "else", is fragile to variable length and spaces, but is a start.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Offline
Thanks for that. I'd been thinking about this overnight and was going to completely rewrite mine.
Going to have a look at it and see if I'd do it differently.
However I can see myself using it with my latex stuff.
Thanks too to Dieter for the idea.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
Actually I had seen that link but his code looked too complicated/NIH, it would be perfect if I could just use an existing preprocessor (ie. the c preprocessor)
Gnu M4 also seems interesting, but given their ifdef syntax (http://www.gnu.org/software/autoconf/ma … html#Ifdef) it doesn't seem suited for large chunks of text
Last edited by Dieter@be (2010-08-06 14:33:21)
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline