3

Imagine we are using some Latex3 command entitled "PersonInformation", which takes two m-andatory and one o-ptional arguments:

\DeclareDocumentCommand \PersonInformation { m m o } {%

...

}

Great. As you may guess, the first m-andatory argument of this command will contain the person's first name.

Now, let's assume that we are going to use this command entitled "PersonInformation" quite often in our document ... that is ... it will be issued for multiple people consecutively.

Each time after the command is closed, there might be some more paragraphs concerning the person, and at the very end of the information on every person, there will be a hyperlink to his profile in some database.

\href{http://a.beautiful.url/?searchid=...}

So, for example, let's assume somebody's first name is NameMeAName. Our instance of "PersonInformation" will then look something as follows:

\PersonInformation{
NameMeAName
}{
%
}[
%
]

Then, there will be some paragraphs describing NameMeAName, and after that we would like to have an URL as follows:

\href{http://a.beautiful.url/?searchid=NameMeAName}

Of course, after person NameMeAName, there might be a new use of the command, for example for person NameMeBName ... which will follow a similar structure.

Now, my question is:

How to not always manually fill in the person's name in \href{http://a.beautiful.url/?searchid=...}, but how to automatically let the URL appear, which will somehow need "to reuse the n-th argument of the last occurrence of another command?" (that's the title of this OP).

In this example case

  • the n-th argument = the first, and mandatory, argument
  • another command = the "PersonInformation" command

Thank you.

MWE

\documentclass[a4paper]{article}
\usepackage{xparse, hyperref}

\DeclareDocumentCommand \PersonInformation { m } {%
\Large {\bfseries#1} \endgraf The person who didn't know why they were named ``#1''. \normalsize
  }

\begin{document}

\PersonInformation{
NameMeAName
}

This person born and died in the same place, same year. \endgraf

On-line biography: \href{http://a.beautiful.url/?searchid=NameMeAName}{Click on me}

\PersonInformation{
NameMeBName
}

This person was born and did not die yet. \endgraf

On-line biography \href{http://a.beautiful.url/?searchid=NameMeBName}{Click on me}

\PersonInformation{
AndSoON
}

This person is not born yet. \endgraf

On-line biography: \href{http://a.beautiful.url/?searchid=AndSoON}{Click on me}

\end{document}
  • just make your listofpeople command define \def\zzz{#1} and then use \zzz in searchid=\zzz – David Carlisle Oct 7 '17 at 11:24
  • @DavidCarlisle Sounds great. How could I do that with the Latex3 version of declaring a command stated above, i.e. \DeclareDocumentCommand \ListOfPeople { m m o } {...} please? – Vincent Mia Edie Verheyen Oct 7 '17 at 11:26
  • well you could use exactly what I wrote or use the l3 version of \def (\tl_set:Nn\l_zzz{#1}) – David Carlisle Oct 7 '17 at 11:30
  • \ListOfPeople seems the wrong name for a command that takes a single person's name? – David Carlisle Oct 7 '17 at 11:31
  • @DavidCarlisle I understand, and changed it into \PersonInformation. – Vincent Mia Edie Verheyen Oct 7 '17 at 11:34
2

Why don’t you use \NewDocumentEnvironment instead of \NewDocumentCommand? You can access the arguments also from within the code that ends the environment. Here is an example of what I mean:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{xparse}
\usepackage{hyperref}

\NewDocumentEnvironment {PersonInformation} { m } {%
    \begingroup
        \Large \textbf{#1}\par
        The person who didn't know why they were named ``#1".\par
    \endgroup
}{%
    \par
    \noindent % say
    On-line biography:
    \href{http://a.beautiful.url/?searchid=#1}{Click on me}\par
}



\begin{document}

Outer text.

\begin{PersonInformation}{PoorBaby}
    This person was born and died in the same place, on the same year.
    (The same year?  Oh, no!)
\end{PersonInformation}

Outer text.

\begin{PersonInformation}{NameMeAName}
    This person was born and did not die yet.
\end{PersonInformation}

Outer text.

\end{document}
  • What if I would like to issue one or more Commands (e.g. \PersonInformationDetails) though (not just a \href), within that Environment (because the commands will need to use the #1st argument of the Environment). My command is currently defined as starting with \def\PersonInformationDetails{\def\... and so on ... but I can't figure out how to incorporate it within the declaration of the environment \NewDocumentEnvironment {PersonInformation} { ... – Vincent Mia Edie Verheyen Oct 8 '17 at 9:10
  • @VincentMiaEdieVerheyen just define then as normal, in the start code of the environment, after the \endgroup which is there to keep the font change local. – David Carlisle Oct 8 '17 at 9:36
  • @DavidCarlisle But how could I refer back to the #1st argument of the encompassing Environment, instead of unintentionally referring back to the #1st argument of the Command? Could you maybe give a dummy example? – Vincent Mia Edie Verheyen Oct 8 '17 at 13:42
  • @VincentMiaEdieVerheyen in a nested definition you need to use ##1 to refer to the inner argument and #1 the outer, this is basic tex syntax search for ## on the site. – David Carlisle Oct 8 '17 at 13:51
  • @DavidCarlisle Wow, that's exciting to learn about. It will be of great help. Thanks a lot. – Vincent Mia Edie Verheyen Oct 8 '17 at 14:39

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.