5

I am writing a document where name of a person A will appear at multiple places section A. Similarly, name of person B will appear at multiple places in section B, etc.

Is there any way by which just before section A, I can define a variable, say, PersonName, assign a value relevant to Person A to it, and use it in section A. Then, redefine PersonName at the beginnig of section B, use it in section B, etc.

If PersonName can not be defined in the body, can I declare it in the preamble and keep assigning different values to it in the body?

1
  • 2
    Have a look at \newcommand as defined by the LaTeX-Kernel. \newcommand{\persona}{Persona Alite} and \newcommand{\personb}{Persona Belite}. Or \newcommand{\person}{alite} \renewcommand{\person}{belite}
    – Johannes_B
    Mar 23, 2014 at 7:48

2 Answers 2

6

This is exactly what macros are for. You can define your own macros with \newcommand{\command}{<actions>}, and redefine them with \renewcommand. Both commands can be given anywhere in the document, so you could do something like this:

\documentclass{article}
\begin{document}
\section{Section A}
\newcommand{\PersonName}{Person A}
Some text written by \PersonName.

\section{Section B}
\renewcommand{\PersonName}{Person B}
Some text written by \PersonName.
\end{document}
2
  • Is there an equivalent of \newcommand in the document body? So that it's just local, like how you might use {\bf yada yada}?
    – Jeff
    Nov 25, 2018 at 20:26
  • This should be a separate question, but has probably been answered here: tex.stackexchange.com/questions/105684/…
    – hugovdberg
    Nov 27, 2018 at 13:53
0

With the 'scontents' package it's easy enough to do what you're looking for. All stored in memory, of course, if you prefer external files, you can use environment version an the key write-env=file.tex and besides storing them in memory, you can have the content in separate files and then use \input. I prefer to save in memory :)

\documentclass{article}
\usepackage{scontents}
\setupsc{store-cmd=person}
\Scontents{Person A}
\Scontents{Person B}
\begin{document}
\section{Section A}
Some text written by \getstored[1]{person}

\section{Section B}
Some text written by \getstored[2]{person}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.