GeneralAdd text to a variable

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
jj2
Posts: 1
Joined: Fri Jul 19, 2013 6:06 pm

Add text to a variable

Post by jj2 »

Hi,

How do i add text to a variable which i defined earlier in the document? I tried it like this:

Code: Select all

\def\exos{some text}
\def\variable{\exos}
\def\exos{\variable added text}
but that doesn't work.

Recommended reading 2024:

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

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

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Add text to a variable

Post by cgnieder »

Hi and welcome to the LaTeX community.

Strictly speaking there are no variables in TeX. There are two kinds of control sequences: TeX primitives and macros. Those macros can either have parameters or be parameter-less. The last type, a parameter-less macro, is the closest thing to a variable.

This means adding something to a macro is redefining the macro in a way that it contains the previous definition (i.e. the once expanded macro). Here is a plain e-TeX way:

Code: Select all

\def\pretovar#1#2{\edef#1{\unexpanded{#2}\unexpanded\expandafter{#1}}}
\def\addtovar#1#2{\edef#1{\unexpanded\expandafter{#1#2}}}

\tt
\def\variable{var}
\meaning\variable

\pretovar\variable{pre}
\meaning\variable

\addtovar\variable{post}
\meaning\variable

\bye
If you're using LaTeX you can use ready-made macros for this by loading the etoolbox package:

Code: Select all

\documentclass{article}
\usepackage{etoolbox}
\begin{document}

\ttfamily
\def\variable{var}
\meaning\variable

\preto\variable{pre}
\meaning\variable

\appto\variable{post}
\meaning\variable

\end{document}
Regards
site moderator & package author
Post Reply