Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Conditional Statement (help!)

38 views
Skip to first unread message

BS

unread,
Mar 19, 1999, 3:00:00 AM3/19/99
to
Greetings,

I want to perform commands based on a variable given
at the beginning of a large document. Basically i want my
document to be compiled in different versions depending
on a variable that I set at the beginning of the document.
There are 6 files included in this file, so it is inconvenient
to go through and comment out lines by hand (inelegant as well, yuck!)

For instance, let version = 1

If version = 1 then do stuff
else if version = 2 then do other stuff.

Is this possible in LaTeX?
I have found some references to \if statements,
but I can't figure out how to use it. More to the point,
I have no idea how to assign a number to a variable (if that
is even possible in TeX).


I am surrounded by LaTeX books, but can't figure out how to
do what I want in LaTeX. I think it may be TeX answer.


cheers,
bob stockwell
stoc...@colorado-research.com

Matt Swift

unread,
Mar 20, 1999, 3:00:00 AM3/20/99
to
In TeX:

\newif\ifhello

where hello is anything you want that's a legal commandname.

to set true:

\hellotrue

to set false:

\hellofalse

to do branching:

\ifhello
text and/or commands if hello is true
\else
text and/or commands if hello is false
\fi

You can leave out the "else" clause.

In LaTeX:

you can use the "ifthenelse" package, which I think is part of the base
distribution; you can see its own documentation.

the moredefs package on CTAN also defines \newboolean and \provideboolean,
but for just them this package is not worth bothering about.

If you want a variable with numbers, use \newcounter, explaied in LaTeX
manual. The ifthenelse package shows how to test counters and so on.
\setcounter{mycount}{value} is the way you set things.

In TeX, there are several other ways to do branching and comparisons and
assignments, but what you described doesn't need them.

BS <Ple...@dont.email.me> wrote in message
news:7cut8a$2...@falcon.ccs.uwo.ca...

GERNOT_KATZER

unread,
Mar 20, 1999, 3:00:00 AM3/20/99
to
BS (Ple...@dont.email.me) wrote:
> Greetings,

> For instance, let version = 1

> If version = 1 then do stuff
> else if version = 2 then do other stuff.

TeX supports binary switches. Of course, you also can
define structures equivalent to multibranch-if. But it's
probably wiser to stay with the simple stuff.

Imagine a document that uses long or short definitions,
and that optionally includes nice photos of the things
to define:

In the preamble, say
\newif \ifshort
\newif \ifbeautiful

You now can set your swtiches to true or false:
\shorttrue \beautifultrue and
\shortfalse \beautifulfalse.
Initially, all switches are false.

In your text, write stuff like the following:

\ifshort
A circle is a round square.
\else
A circle is defined by its equation $x^2+y^2=r^2$
and is invariant to any rotation.
\ifbeautiful
\includegraphics {....}
\fi
\fi

Gernot

Donald Arseneau

unread,
Mar 21, 1999, 3:00:00 AM3/21/99
to
In article <7cut8a$2...@falcon.ccs.uwo.ca>, "BS" <Ple...@dont.email.me> writes...
(I will email you against your wishes:-)

>I want to perform commands based on a variable given
>at the beginning of a large document.

People suggested using \ifsomething directly.
Isn't there a LaTeX package to handle this? version.sty maybe?

I have a very old package that you can try. If it still works in
LaTeX2e, tell me and I will send it to ctan.

Donald Arseneau as...@triumf.ca

BS

unread,
Mar 21, 1999, 3:00:00 AM3/21/99
to
Greetings all,

thanks to everyone who answered. It was very helpful.
In case anyone cares, here is a summary of the problem
and the solution (its pretty simple).

The problem: I want to have a latex file for my thesis that
compiles the entire thesis, including all chapters and appendices.
But I want to edit each indivdual file (chapter) without having to
fuss around with commenting out lines like \begin{document} etc.
That is, I want to include files whole stand alone compilable latex
files (with preambles) into my "master" latex document.


Here is the solution I settled on:


In the "master file" called mythesis.tex I have the following definition
for "\setflag",
and a bunch of includes:

%%%%%%%%start of file

\newcommand{\setflag}{\newif \ifwhole
\wholetrue}

\documentclass[12pt]{thesis}
\usepackage{psfig}
\usepackage[square]{natbib}
\begin{document}

\bibliographystyle{plainnat}
\input{chapter0}
\newpage \addcontentsline{toc}{chapter}{Table of Contents}
\tableofcontents
\newpage \addcontentsline{toc}{chapter}{List of Figures}
\listoffigures
\newpage \addcontentsline{toc}{chapter}{List of Tables}
\listoftables


\include{chapter1.tex}
\include{chapter2.tex}
\include{chapter3.tex}
\include{chapter4.tex}
\include{chapter5.tex}
\include{chapter6.tex}
\include{chapter7.tex}
\include{append1.tex}
\include{append2.tex}
\include{append3.tex}
\include{append4.tex}
(etc)

\end{document}
%%%%%%%%end of file

Then in each chapter I have the following conditional definition
for "\setflag":

%%%%%%%%start of file

\providecommand{\setflag}{\newif \ifwhole
\wholefalse}
\setflag

\ifwhole\else
\documentclass[12pt]{thesis}
\usepackage{psfig}
\usepackage[square]{natbib}

\pssilent
\renewcommand{\baselinestretch}{1.5} \small\normalsize
\pagenumbering{arabic}
\pagestyle{myheadings}
\setcounter{page}{54}
\setcounter{chapter}{3}
\begin{document}
\fi

%%%%%%%%end of file

Thus, if I compile chapter1.tex, it compiles, no problem.
The "if" command is executed (based on who is calling for
this file).
Also, I can compile mythesis.tex, and it includes chapter1.tex, and
the "if" commands are not executed.
Thus it compiles the whole thing.
Sweeeet!

cheers,
bob

tonyb...@yahoo.com

unread,
Mar 22, 1999, 3:00:00 AM3/22/99
to
"BS" <Ple...@dont.email.me> wrote:
> The problem: I want to have a latex file for my thesis that
> compiles the entire thesis, including all chapters and appendices.
> But I want to edit each indivdual file (chapter) without having to
> fuss around with commenting out lines like \begin{document} etc.
> That is, I want to include files whole stand alone compilable latex
> files (with preambles) into my "master" latex document.

Why don't you just use \include commands for each chapter
and then use \includeonly. This is exactly what it was
designed for.

> \usepackage{psfig}

Why in the world are you still using psfig? psfig is
buggy unsupported code which was replaced by \includegraphics
over 5 years ago now.

Tony

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

BS

unread,
Mar 22, 1999, 3:00:00 AM3/22/99
to
tonyb...@yahoo.com wrote:
>"BS" <Ple...@dont.email.me> wrote:
>> The problem: I want to have a latex file for my thesis that
>> compiles the entire thesis, including all chapters and appendices.

>


>Why don't you just use \include commands for each chapter
>and then use \includeonly. This is exactly what it was
>designed for.

On my system, it is very convenient to compile the file I'm editing.
So I open up chapter4.tex, type a paragraph, compile to make sure I've
not made a typo and get an error, repeat,etc. I only work on the one file.
Then to do the whole thesis, I just compile mythesis.tex.
The file mythesis.tex never changes. No commenting out lines etc.

I like that more than opening up the mythesis.tex file, commenting out
lines,
switching over to chapter4.tex making additions and revisions, switching
back to mythesis.tex.
etc. Of course, it is very easy to use the include packages, but I prefer
this method
of being able to include files that are stand alone.

I find that this is more convenient too when dealing with multiple authors
too.
That way we only share one file per chapter, and it is easy to separate who
is working on what.


I figure I save about 10 "having-to-type-%-to-comment-out-lines",
and two alt-tabs per compile. Not much. But also, not zero.


Thanks for your comments.

>> \usepackage{psfig}
>
>Why in the world are you still using psfig?

In the world, I'm sticking with the "not broke - don't fix" rule.
I came across psfig first, and it has worked fine. I included about
130 postscript files in my thesis, and there is no problem.
I am aware of the time honoured existence of \includegraphics.
Thanks for the pointer.


0 new messages