如何定義我自己的數數?

如何定義我自己的數數?

我想定義我自己的計數及其顯示。

例如,

今天我在停車場遇見了大衛(P1)。大衛很友善。突然我想起他是我的朋友蘇珊(P2)。 …

如果我透過添加來編輯文本

今天我在停車場遇見了大衛(P1)。大衛很友善。大衛是約翰的父親(P2)。突然我想起他是我的朋友蘇珊(P3)。 …

我希望數字會自動改變。

答案1

也許是這樣的?

\documentclass{article}
\newcounter{numerations}
\newcommand\nlabel[1]{%
  \refstepcounter{numerations}%
  \expandafter\xdef\csname REF#1\endcsname{\thenumerations}%
  \expandafter\xdef\csname NUMERATIONS\thenumerations\endcsname{#1}%
  #1 (P\thenumerations)%
}
\newcommand\nref[1]{#1 (P\csname REF#1\endcsname)}
\newcommand\numeration[1]{\csname NUMERATIONS#1\endcsname{} (P#1)}
\begin{document}
Today I have met \nlabel{David} in the parking.  David was friendly.
\textit{David is the father of \nlabel{John}.} Suddenly I remember he was
a friend of mine \nlabel{Susan}...

I can recall the label for \nref{John}, \nref{David}, and or \nref{Susan}.

Or I can invoke them by number: \numeration{2}, \numeration{1}, and or 
\numeration{3}.

\end{document}

在此輸入影像描述

答案2

這是一個帶有 - 工作(如果需要)和使用可選參數自動標記的版本\nameref,該參數指的是人的名字,而附加\label命令巨集\newpersons將引用此人的號碼(即 P1 等)

\documentclass{article}


\usepackage{xparse}

\newcounter{person}
\renewcommand{\theperson}{P\arabic{person}}
\makeatletter
\NewDocumentCommand{\newperson}{O{#2}m}{%
  \protected@edef\@currentlabel{#1}%
  % If hyperref is not loaded, \protected@edef\currentlabelname does no harm here
  \protected@edef\@currentlabelname{#1}% for nameref. 
  \label{#2}%
  \refstepcounter{person}%
  #1~(\theperson)%
}
\makeatother

\usepackage{hyperref}

\begin{document}
Today I have met \newperson{David} in the parking.  David was friendly.
\textit{\ref{David} is the father of \ref{John}.} Suddenly I remember he was
a friend of mine \newperson{Susan}\label{susansnumber}...

I can recall the label for \newperson{John}, \ref{David}, and or \nameref{Susan} or call them by a reference to the number with \ref{susansnumber}. 

\end{document}

在此輸入影像描述

答案3

以下範例提供了\newperson{<name>}設定名稱後跟遞增計數器的形式(P<num>)

在此輸入影像描述

\documentclass{article}

\newcounter{person}
\newcommand{\newperson}[1]{%
  \refstepcounter{person}%
  #1~(P\theperson)}

\begin{document}

Today I have met David~(P1) in the parking. David was friendly. 
David is the father of John~(P2). Suddenly I remember he was a friend of mine, Susan~(P3).

Today I have met \newperson{David} in the parking. David was friendly. 
David is the father of \newperson{John}. Suddenly I remember he was a friend of mine, \newperson{Susan}.

\end{document}

這可以擴展到使用\label-\ref系統,允許人們引用已經命名的人(不是新的),以保持一致性。這是這樣的實現\newperson[<tag>]{<name>},可以參考使用\refperson{<tag>}.如果沒有<tag>提供,<name>則使用:

在此輸入影像描述

\documentclass{article}

\newcounter{person}
\makeatletter
\newcommand{\newperson}[2][]{%
  \refstepcounter{person}% New person added
  \def\@currentlabel{#2}% Update label
  \@currentlabel~(P\theperson)% Set person
  % https://tex.stackexchange.com/a/53091/5764
  \if\relax\detokenize{#1}\relax
    \label{#2}%
  \else
    \label{#1}%
  \fi
  }
\makeatother
\newcommand{\refperson}[1]{\ref{#1}}%

\begin{document}

Today I have met Davidofilofsky~(P1) in the parking. Davidofilofsky was friendly. 
Davidofilofsky is the father of John~(P2). Suddenly I remember he was a friend of mine, Susan~(P3).

Today I have met \newperson[david]{Davidofilofsky} in the parking. \refperson{david} was friendly. 
\refperson{david} is the father of \newperson{John}. Suddenly I remember he was a friend of mine, \newperson{Susan}.

\end{document}

相關內容