1

I need to have a macro that looks at its previous use and compares the first arguments, then prints additional text if those arguments are the same.

For example

\mycommand{Michael}{Hello, John}

Michael and John shake hands.

\mycommand{Michael}{How are you today?}\\
\mycommand{John}{I'm good, how are you, Michael?}\\
\mycommand{Michael}{Good, thank you.}

would print something like:

Michael: Hello, John.

Michael and John shake hands.

Michael (again): How are you today?
John: I'm good, how are you, Michael?
Michael: Good, thank you.

I initially thought I might be able to use something like \renewcommand{\charactername}{#1} in the macro definition, but that would need to be expanded for the comparison to work. Any ideas?

EDIT

@egreg answered below with a solution that worked first go. If you need to reset the stored argument (in my example, the names of characters) you can use \gdef\dialogue@remember{}.

  • Look at some acronym package (acro, glossaries), they all contains commands to print something different at the first usage. – Ulrike Fischer Jul 18 '17 at 9:29
  • @UlrikeFischer — It's the subsequent usage that needs to change, and only for sequential use of the same command with identical first arguments. – experimenthouse Jul 18 '17 at 9:33
  • It is the same principle: The command does something different depending on previous usage. – Ulrike Fischer Jul 18 '17 at 9:38
1

Store in memory the previous speaker:

\documentclass{article}
\usepackage{pdftexcmds} % for engine cross-compatibility

\makeatletter
\newcommand{\mycommand}[2]{%
  #1\ifnum\pdf@strcmp{\mycommand@remember}{#1}=\z@ ~(again)\fi
  \gdef\mycommand@remember{#1}%
  : #2%
}
\def\mycommand@remember{} % initialize
\makeatother

\setlength{\parindent}{0pt} % just for the example

\begin{document}

\mycommand{Michael}{Hello, John}

Michael and John shake hands.

\mycommand{Michael}{How are you today?}

\mycommand{John}{I'm good, how are you, Michael?}

\mycommand{Michael}{Good, thank you.}

\end{document}

enter image description here

An extended version where the main command has a *-variant that clears the remembered character.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\dialogue}{smm}
 {
  \IfBooleanT { #1 } { \str_gclear:N \g__experimenthouse_speaker_str }
  \experimenthouse_dialogue:nn { #2 } { #3 }
 }

\str_new:N \g__experimenthouse_speaker_str

\cs_new_protected:Nn \experimenthouse_dialogue:nn
 {
  #1 % speaker
  \str_if_eq:VnT \g__experimenthouse_speaker_str { #1 } { ~(again) }
  \str_gset:Nx \g__experimenthouse_speaker_str { \tl_to_str:n { #1 } }
  :~#2
 }

\ExplSyntaxOff

\setlength{\parindent}{0pt} % just for the example

\begin{document}

\dialogue{Michael}{Hello, John}

Michael and John shake hands.

\dialogue{Michael}{How are you today?}

\dialogue{John}{I'm fine, how are you, Michael?}

\dialogue{Michael}{Fine, thank you.}

\dialogue{Michael}{Fine, thank you.}

\dialogue*{Michael}{Fine, thank you.}

\end{document}

enter image description here

  • — Wow! This does exactly what I wanted. Extremely helpful. Thank you very much! – experimenthouse Jul 18 '17 at 10:27

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.