GeneralDefining / Redefining Variables

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
eleanor
Posts: 19
Joined: Sat Oct 03, 2009 6:03 pm

Defining / Redefining Variables

Post by eleanor »

Hi.

I would like to achieve something like this: I have defined a new command with \newcommand and I'm passing an argument to it, like this: pdf, text, doc - which is the filetype. So what I want to achieve is to define a variable like this:

Code: Select all

\def\filetype{text}
And then compare which extension was inputed like this:

Code: Select all

\ifthenelse{\equal{#1}{pdf}}{\let\filetype{pdf}}
The problem is that the latter redefinition of the filetype variable doesn't work. Does anybody have any idea how can I achieve this.

Thanks in advance

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10348
Joined: Mon Mar 10, 2008 9:44 pm

Re: Defining / Redefining Variables

Post by Stefan Kottwitz »

Hi Eleanor,

you can use \newcommand do define a macro and \renewcommand to redefine the macro later. These are LaTeX macros, whereas \def and \let are TeX primitives.

Stefan
LaTeX.org admin
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Defining / Redefining Variables

Post by cgnieder »

Although this is a rather old question I'd like to explain in a little more detail why

Code: Select all

\ifthenelse{\equal{#1}{pdf}}{\let\filetype{pdf}}
can't work:
The problem is \let\filetype{pdf}; \let can only define one token to be another one.

Code: Select all

\let<token1><token2>
or

Code: Select all

\let<token1>=<token2>
An example would be something like this:

Code: Select all

\def\foo{foo}%
\def\bar{bar}%
\let\foo\bar
\show\foo
gives (in the log):
> \foo=macro:
->bar.
In the case at hand one can use for instance \def instead:

Code: Select all

\documentclass{article}
\usepackage{ifthen}
\def\filetype{tex}
\newcommand*\setfiletype[1]{%
  \ifthenelse{\equal{#1}{pdf}}{\def\filetype{pdf}}{}}
\begin{document} 

\setfiletype{dvi}
\filetype

\setfiletype{pdf}
\filetype

\end{document}
This will give:
tex
pdf
Regards
site moderator & package author
Post Reply