如果描述中使用了至少两次,则自动对单词进行编号

如果描述中使用了至少两次,则自动对单词进行编号

我定义了一个环境,description如果粗体单词在 中出现两次或更多次,则会自动对其进行编号\item[]

到目前为止它运行良好,但1如果单词以“不正确”的顺序出现得更频繁,我就无法在第一个单词中添加 a(请参阅 ME)。

这是我尽可能简单的例子以及我到目前为止所做的工作(需要两次运行):

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage{selinput}
\SelectInputMappings{
adieresis={ä}, germandbls={ß},
}
\usepackage{xstring}
\usepackage{blindtext}

%%%%
\makeatletter

\newcounter{dcounter}
\newcounter{ecounter}

  \def\Tipps@item[#1]{\g@addto@macro{\ListeII}{[#1]}%
  \if@noparitem
    \@donoparitem
  \else
    \if@inlabel
      \indent \par
    \fi
    \ifhmode
      \unskip\unskip \par
    \fi
    \if@newlist
      \if@nobreak
        \@nbitem
      \else
        \addpenalty\@beginparpenalty
        \addvspace\@topsep
        \addvspace{-\parskip}%
      \fi
    \else
      \addpenalty\@itempenalty
      \addvspace\itemsep
    \fi
    \global\@inlabeltrue
  \fi
  \everypar{%
    \@minipagefalse
    \global\@newlistfalse
    \if@inlabel
      \global\@inlabelfalse
      {\setbox\z@\lastbox
       \ifvoid\z@
         \kern-\itemindent
       \fi}%
      \box\@labels
      \penalty\z@
    \fi
    \if@nobreak
      \@nobreakfalse
      \clubpenalty \@M
    \else
      \clubpenalty \@clubpenalty
      \everypar{}%
    \fi}%
  \if@noitemarg
    \@noitemargfalse
    \if@nmbrlist
      \refstepcounter\@listctr
    \fi
  \fi
  \sbox\@tempboxa{\makelabel{#1%
    % Is #1 on the list?
\IfSubStr{\Liste}{#1}{%
    % If it is
\def\inhalt{#1}%
% How often is #1 in \ListeII
\StrCount{\ListeII}{[#1]}[\Number]%
% If \Number is 1 do nothing, else print \Number
\ifnum\Number=1\relax\else~\Number%
  % If Number is 2 make a label
  \ifnum\Number=2 \label{\thedcounter\theecounter}\fi%
\fi%
}{%
% If not: Add to list
\g@addto@macro{\Liste}{ #1 }%
\stepcounter{dcounter}%
% is label r@\thedcounter\theecounter defined or not?
\@ifundefined{r@\thedcounter\theecounter}{}{~1}%
}%
:}}%
  \global\setbox\@labels\hbox{%
    \unhbox\@labels
    \hskip \itemindent
    \hskip -\labelwidth
    \hskip -\labelsep
    \ifdim \wd\@tempboxa >\labelwidth
      \box\@tempboxa
    \else
      \hbox to\labelwidth {\unhbox\@tempboxa}%
    \fi
    \hskip \labelsep}%
  \ignorespaces}

\def\Tippsitem{% 
  \@inmatherr\Tippsitem 
  \@ifnextchar [\Tipps@item{\@noitemargtrue \Tipps@item[\@itemlabel]}} 

% - - - - - - - - - - - - - - - - - - - - - - 

\newenvironment{Hinweis}{\def\Liste{}\def\ListeII{}% make lists empty
\setcounter{dcounter}{1}% setcounter back to one
\stepcounter{ecounter}%
\let\item\Tippsitem% 
\begin{description}} 
{\end{description}} 

\makeatother
%%%%%

\begin{document}

% In "correct" order -> works fine :)
\begin{Hinweis}
\item[Fuß] In "correct" order -> works fine :)
\item[Fuß] For the problem look at the next list
\item[Fuß]correct
\item[was]correct
\item[Ärger]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Hmmm\dots]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\end{Hinweis}



And here is the problem:
 % In "incorrect" order -> doesn't work fine :(
\begin{Hinweis}
\item[Fuß] Here should be a \textbf{1}
\item[Ärger] No 1
\item[Fuß] correct
\item[Fuß] correct
\item[Hmmm\dots] correct
\item[Würfel] Should be a \textbf{1}
\item[was] No \textbf{1}
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\end{Hinweis}

\end{document}

如您所见,第二个列表的编号是错误的。

你可以找到我的第一次尝试这里 (第二篇帖子),但我不喜欢这个解决方案,因为它不允许我使用\dots,而且我必须构建一个宏才能使用ß等等......所以我做了一个新的(这个)。

我希望有人知道如何解决这个数字问题。

答案1

这里有一种方法environ

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xstring}
\usepackage{blindtext}
\usepackage{environ}

%%%%
\makeatletter
\NewEnviron{Hinweis}{%
  \setbox0=\vbox{%
    \def\hinw@list{}
    \let\item\hinw@tempitem
    \BODY\expandafter}%
  \expandafter\def\expandafter\hinw@list\expandafter{\hinw@list}
  \def\do##1{\@namedef{hinw@\detokenize{##1}}{0}}\hinw@list
  \let\latex@item\item
  \let\item\hinw@item
  \begin{description}\BODY\end{description}}
\def\hinw@add#1{%
  \expandafter\def\expandafter\hinw@list\expandafter{\hinw@list\do{#1}}}
\def\hinw@tempitem[#1]{%
  \@ifundefined{hinw@\detokenize{#1}}
    {\@namedef{hinw@\detokenize{#1}}{}}{\hinw@add{#1}}%
}
\def\hinw@item[#1]{%
  \@ifundefined{hinw@\detokenize{#1}}
    {\latex@item[#1]}
    {\@nameedef{hinw@\detokenize{#1}}{\number\numexpr\@nameuse{hinw@\detokenize{#1}}+1\relax}%
     \latex@item[#1 \@nameuse{hinw@\detokenize{#1}}]}}
\providecommand\@nameedef[1]{\expandafter\edef\csname#1\endcsname}
\makeatother

%%%%%

\begin{document}

% In "correct" order -> works fine :)
\begin{Hinweis}
\item[Fuß] In "correct" order -> works fine :)
\item[Fuß] For the problem look at the next list
\item[Fuß]correct
\item[was]correct
\item[Ärger]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Hmmm\dots]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\end{Hinweis}



And here is the problem:
 % In "incorrect" order -> doesn't work fine :(
\begin{Hinweis}
\item[Fuß] Here should be a \textbf{1}
\item[Ärger] No 1
\item[Fuß] correct
\item[Fuß] correct
\item[Hmmm\dots] correct
\item[Würfel] Should be a \textbf{1}
\item[was] No \textbf{1}
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\item[Würfel]correct
\end{Hinweis}

\end{document}

整个环境首先排版在一个临时框中,其中检查各种项目名称,如果其中一个至少出现两次,则将其存储在列表中;之后,该列表用于定义扩展到数字的合适命令。使用\detokenize应该使其独立于文档编码。这里我使用了 UTF-8,但它也适用于 Latin-1(已测试)。

在此处输入图片描述

相关内容