拥有非连续的唯一标签吗?

拥有非连续的唯一标签吗?

代码

\documentclass{article}
\usepackage{enumitem}

Nephritic syndrome
\begin{enumerate}[label=\textbf{S.\arabic*}]

\item Lorem ipsun. \label{treatmentOne} 

\end Hello world!

\end{enumerate}

See the treatment option \ref{treatmentOne}. 

\end{document}

给出

Nephritic syndrome

    S.1 Lorem ipsun 

    S.2 Hello World!

See the treatment option S.1.

但是,我希望有由字母和数字组成的独特非序列标签。我不喜欢枚举的序列标识。如果能以某种方式随机化或以某种方式模仿给定的单词,那就太好了。如何在 中自动生成非序列标签enumerate

答案1

请参阅本文末尾的枚举标签的真正随机化版本!

\documentclass{article}

\usepackage{pgffor}
\usepackage{enumitem}



\makeatletter
\newcommand{\@randlabelprefix}{%
  Nephr%
}

\newcommand{\SetRandLabelPrefix}[1]{%
  \renewcommand{\@randlabelprefix}{#1}%
}




\newcommand{\GetRandLabelPrefix}{%
  \@ifundefined{@randlabelprefix}{%
    Nephr%
  }{%
    \@randlabelprefix%
  }%    
}
\makeatother


\newlist{randenum}{enumerate}{1}
\setlist[randenum,1]{label={\GetRandLabelPrefix\arabic*.},widest={\GetRandLabelPrefixAAAA}}





\begin{document}
\begin{randenum}
\item First  \label{Somelabel}
\item Second \label{otherlabel}
\item Third  \label{anotherone}
\foreach \x in {4,...,10} {%
  \item \therandenumi
  }
\end{randenum}

In \ref{Somelabel} and in \ref{otherlabel} and even in \ref{anotherone}

\end{document}

在此处输入图片描述

编辑

使用随机后缀进行更新 - 但是,这不适用于\label\ref(目前为止)。由于字母的间距不同,标签的宽度会发生变化 - 在这种情况下,等宽字体会更好。

我想,为了使它发挥作用,\ref我必须重新定义它。p@

\documentclass{article}

\usepackage{pgf,tikz}
\usepackage[seed=100,counter=myrandcounter,first=0,last=9]{lcg}
\usepackage{pgffor}
\usepackage{enumitem}




\makeatletter
\newcommand{\@randlabelprefix}{%
  Neph%
}

\newcommand{\SetRandLabelPrefix}[1]{%
  \renewcommand{\@randlabelprefix}{#1}%
}


\DeclareRobustCommand{\dorandit}[1]{%
  \foreach \x in {1,...,#1} {%
    \rand\alph{myrandcounter}%
  }%
}



\newcommand{\GetRandLabelPrefix}{%
  \@ifundefined{@randlabelprefix}{%
    Neph%
  }{%
    \@randlabelprefix%
  }%
  \dorandit{5}%
}
\makeatother


\newlist{randenum}{enumerate}{1}
\setlist[randenum,1]{label={\GetRandLabelPrefix.},widest={AAAAAAAA}}



\begin{document}
\begin{randenum}
\foreach \x in {1,...,20} {%
  \item \therandenumi
  }
\end{randenum}


\end{document}

在此处输入图片描述

最终更新:引用正确,但仍然不太美观,而且目前只适用于一级枚举

它提供了一个 FooXYZ 标签系统,其中 Foo 可以用 来设置\SetRandLabelPrefix

一些描述:您可以使用 设置数字的数量\SetRandDigits

目前,为了使其工作,请使用\randenumlabel{somelabel}而不是\label{somelabel}

\documentclass{article}



\usepackage[seed=100,counter=randenumcounter,first=1,last=26]{lcg}
\usepackage{pgffor}
\usepackage{xpatch}
\usepackage{enumitem}

\usepackage{multicol} % Just for testing




\makeatletter
\newcommand{\@randlabelprefix}{%
  Neph%
}

\newwrite\randenum@storeenum
\newread\randenum@readenum



\providecommand{\@numofdigits}{%
  3%
}

\newcommand{\SetRandDigits}[1]{%
  \renewcommand{\@numofdigits}{#1}
}

\newcommand{\GetRandDigits}{%
  \@numofdigits%
}

\newcommand{\RandCounterFormat}[1]{%
  \alph{#1}%
}

\newcommand\readrandlabel[2]{%
  \IfFileExists{#2}{%
    \openin#1=#2%
    \read#1 to \randlabel@curcontent%
    \closein#1%
  }{%
  }%
}


\def\randlabel@curcontent{}
\xpretocmd{\refstepcounter}{\readrandlabel{\randenum@readenum}{\jobname.rand}}{}{}

\newcommand{\randenumlabel}[1]{%
  \protected@edef\@currentlabel
  {\csname p@randenumi\endcsname\randlabel@curcontent}%
  \label{#1}%
}


\newcommand{\SetRandLabelPrefix}[1]{%
  \renewcommand{\@randlabelprefix}{#1}%
}



\DeclareRobustCommand{\dorandit}[1]{%  Must be robust due to \foreach and \rand
  \immediate\openout\randenum@storeenum=\jobname.rand
  \def\a{\@randlabelprefix}%
  \foreach \x in {1,...,#1} {%
    \rand%
    \xdef\a{\a\RandCounterFormat{randenumcounter}}% recursive built - up of the 
  }%
  \immediate\write\randenum@storeenum{%
    \a% write to the file
  }%
  \immediate\closeout\randenum@storeenum% close file
}


% Now some commands to provide `\RandLabel*` label format for `enumitem` lists

\newcommand{\GetRandLabelPrefixHelper}[1]{%  
  \randlabel@curcontent%
}

\newcommand{\RandLabel}[1]{%
  \dorandit{\GetRandDigits}%
  \GetRandLabelPrefixHelper{#1}%
}


\AddEnumerateCounter{\RandLabel}{\GetRandLabelPrefixHelper}{AAAAAAAAAAAAA}
\newlist{randenum}{enumerate}{1}
\setlist[randenum,1]{label={\ttfamily\RandLabel*},widest={AAAAAAA}}

\makeatother



\begin{document}

\begin{multicols}{2}
\begin{randenum}
\foreach \x in {1,...,20} {%
\item \x  \randenumlabel{re::\x} $\longrightarrow$ \ref{re::\x} % Test the labels
}
\end{randenum}

\begin{enumerate}
  \foreach \x in {1,...,20} {%
  \item \x \label{normal::\x}
  }
\end{enumerate}
\end{multicols}

In \ref{re::15}  and in \ref{re::20} we see the correct labels 

In \ref{normal::15} and in \ref{normal::20} we see the correct labels (too)

\end{document}

在此处输入图片描述

相关内容