Text Formattingmacro first occurance

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
kalle1
Posts: 2
Joined: Thu Sep 19, 2013 8:51 pm

macro first occurance

Post by kalle1 »

Hi together,
I'm searching for a macro which highlights only the first occurance of a marked word. The idea is to automatize the highlighting of the first occurance of an author, e.g. I imagine the resulting text
While Firstname R. Lastname explains first, ...
Firstname R. Lastname later revokes her/his theory...
to be marked up like this:
While \author{Firstname R. Lastname} explains first, ...
\subsection{bla}
\author{Firstname. R. Lastname} later revokes ...

Even nicer would be an id to allow individual content in the author element, e.g.
while \author[lastname]{Firstname R. Lastname] explains first, ...
\subsection{bla}
\author[lastname]{Lastname} later revokes ...
.
Has anyone every seen such an extension? If it doesn't exists I'll have to write my own macro. This question only aims to find out about existing resources to avoid the reinvention of the wheel.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

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

macro first occurance

Post by cgnieder »

Hi kalle1,

The acronym and glossaries (acro, acronym, glossaries...) packages do such things but I don't believe that they're perfectly suited for your needs. It is possible without too much hassles to define such macros yourself. One way would be to define a boolean on a per-name basis that is set to true on the first use and use a different formatting depending on the state of the boolean.

Here is a preliminary code example that implements \newauthor{<id>}{<first name>}{<last name>} that defines a name, and \useauthor{<id>} that prints the full defined name as well as \useauthor*{<id>} which only prints the last name. I tried to separate usage from formatting so a few simple macros are defined for the formatting where you can choose a formatting you deem best.

Code: Select all

\documentclass{article}
% a font that has italic small caps:
\usepackage[T1]{fontenc}
\usepackage{libertine}

% programming tools:
\usepackage{etoolbox}
\makeatletter
% user level command for defining authors:
\newrobustcmd\newauthor[3]{%
  \kalle@new@author{#1}{#2}{#3}%
}
% internal command for defining authors:
\def\kalle@new@author#1#2#3{%
  % boolean to detrmine first and further use:
  \newbool{kalle@author@#1@used}%
  % un-starred variant typesets the full name:
  \csdef{kalle@author@#1@nostar}{%
    \ifbool{kalle@author@#1@used}
      {% further use:
        \begingroup
          \kalle@further@format
          #2 {\kalle@last@name@format#3}%
        \endgroup
      }
      {% first use:
        \booltrue{kalle@author@#1@used}%
        \begingroup
          \kalle@first@format
          #2 {\kalle@last@name@format#3}%
        \endgroup
      }%
  }%
  % starred variant typesets only the last name:
  \csdef{kalle@author@#1@star}{%
    \ifbool{kalle@author@#1@used}
      {% further use:
        \begingroup
          \kalle@further@format
          \kalle@last@name@format
          #3%
        \endgroup
      }
      {% first use:
        \booltrue{kalle@author@#1@used}%
        \begingroup
          \kalle@first@format
          \kalle@last@name@format
          #3%
        \endgroup
      }%
  }%
}
% user level for using an author:
\newrobustcmd\useauthor{%
  \kalle@use@author
}
% internal command for using an author; call the last or the full name
% depending on an optional star:
\def\kalle@use@author{%
  \@ifstar
    {\kalle@use@author@star}
    {\kalle@use@author@nostar}%
}
\def\kalle@use@author@star#1{%
  \csuse{kalle@author@#1@star}
}
\def\kalle@use@author@nostar#1{%
  \csuse{kalle@author@#1@nostar}
}
% define the initial formats of first and further use, also a special
% formatting for the last name:
\def\kalle@first@format{\itshape}
\def\kalle@further@format{\upshape}
\def\kalle@last@name@format{\scshape}
\makeatother

\newauthor{lastname}{Firstname R.\@}{Lastname}
\newauthor{doe}{John}{Doe}

\begin{document}

first \useauthor{lastname} and later \useauthor{lastname}

first \useauthor*{doe} and later \useauthor*{doe}

\end{document}
authors.png
authors.png (12.43 KiB) Viewed 3496 times
Regards
site moderator & package author
Post Reply