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?

lockstep
  • 250,273
deshmukh
  • 2,435
  • 1
  • 26
  • 46
  • 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 '14 at 07:48

2 Answers2

7

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}
hugovdberg
  • 1,418
  • 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 '18 at 20:26
  • This should be a separate question, but has probably been answered here: https://tex.stackexchange.com/questions/105684/limit-macro-newcommand-scope-to-section – hugovdberg Nov 27 '18 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}

Paul Wintz
  • 402
  • 2
  • 14