GeneralDefining and reusing variables

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
sierra1bravo
Posts: 11
Joined: Sun Jan 04, 2009 7:28 am

Defining and reusing variables

Post by sierra1bravo »

Dear all
How do I define a variable and then reuse it later in the document? Something like:

Code: Select all

\def\MyVariable{\value{fboxsep}}
\setlength{\fboxsep}{20pt}
%% Do some stuff

% Now restore the value
\setlength{\fboxsep}{\MyVariable}
Also, how can I define a variable in one place and reuse it as a parameter
in multiple places? For example, I have many charts in my document, and I'd
like to place their width into a variable so that I can change it in just one place. Something like

Code: Select all

\def\MyWidth{0.8\textwidth}
%%%%
\includegraphics[width=MyWidth]{newimages/p002.png} 
Thanks in advance

sb

Recommended reading 2024:

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

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

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Defining and reusing variables

Post by localghost »

The first problem is solved by defining a new length, setting it to \fboxsep and use it later on to reset \fboxsep.

Code: Select all

\newlength{\mylength}
\setlength{\mylength}{\fboxsep}   % Saving fboxsep into mylength
\setlength{\fboxsep}{20pt}
...
\setlength{\fboxsep}{\mylength}   % restoring fboxsep
The other issue is solved very similar to your approach.

Code: Select all

\newcommand*{\factor}{0.8}
\includegraphics[width=\factor\linewidth]{filename}
\includegraphics[scale=\factor]{filename}

Best regards
Thorsten¹
sierra1bravo
Posts: 11
Joined: Sun Jan 04, 2009 7:28 am

Defining and reusing variables

Post by sierra1bravo »

localghost wrote:The first problem is solved by defining a new length, setting it to \fboxsep and use it later on to reset \fboxsep.

Code: Select all

\newlength{\mylength}
\setlength{\mylength}{\fboxsep}   % Saving fboxsep into mylength
\setlength{\fboxsep}{20pt}
...
\setlength{\fboxsep}{\mylength}   % restoring fboxsep
Best regards
Thorsten
Thanks very much. A related query: how can one achieve the following?

\newcommand*{\bufferspace}{8pt}

\vspace{\bufferspace}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Defining and reusing variables

Post by localghost »

sierra1bravo wrote:[...] how can one achieve the following?

\newcommand*{\bufferspace}{8pt}

\vspace{\bufferspace}
You need to learn the difference between a command and a length.

Code: Select all

\newlength{\bufferspace}
\setlength{\bufferspace}{8pt}
\vspace{\bufferspace}
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Defining and reusing variables

Post by phi »

sierra1bravo wrote:Thanks very much. A related query: how can one achieve the following?

\newcommand*{\bufferspace}{8pt}

\vspace{\bufferspace}
This code is fine and works as expected. However, keep in mind that \vspace inside a paragraph adds vertical space after the current line, while after a paragraph it adds space between the last and the next paragraph.
Post Reply