自动创建标签所需的宏

自动创建标签所需的宏

我请求帮助在给定文本的命令内创建标签。给定的文本包含空格和变音符号。请参阅我的 MWE,其中包含我的问题:

\documentclass[pagesize, english, fontsize=12pt, parskip=half]{scrartcl}
\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}
\usepackage{babel, xspace, tocbasic}



\newcounter{Anl}
\newcommand{\Anlv}[1]{%
  \refstepcounter{Anl}%
  \textit{attachment \theAnl{}\xspace}%
  \addtocontents{anl}{%
    \mdseries{}Attachment \theAnl{}: #1\newline%{}{}%
  }%
%  \label{ANL:#1}% REMOVE THE FIRST % AND YOU GET AN ERROR
}
\DeclareNewTOC[%
type=anlverz,%
types=anlverzes,%
nonfloat,%
name=Anlagen,%
listname={Attachments:}%
]{anl}



\begin{document}

Above you see a new command which I use to add attachments to my
texts. Often I need to refer to an attachment, e.g. see the Map of Özß
(\Anlv{Map of Özß, map scale 1:2500}).

I'd like to get a label automatically. But as soon as there are spaces
in the name (here: »Map of Özß, map scale 1:2500«) or an umlaut or an
»ß«, I get an error and no label.

Can anybody write a macro which constructs a usefull label out of the
given name of an attachment? Here in my example »Map of Özß, map scale
1:2500« simply the first word (»Map«) as a label (\verb"\label{ANL:Map}")
would be an improvement, but there may be more than one attachment
with a map. So the best solution for my example, if possible, were:
\verb"\label{ANL:Map-of-Oezss}", because you can replace an »Ö« with
»Oe« and an »ß« with »ss«.


\listoftoc{anl}

\end{document} 

答案1

以下示例将有问题的字符串转换为可以使用的十六进制字符串\label

\documentclass[pagesize, english, fontsize=12pt, parskip=half]{scrartcl}
\usepackage[T1]{fontenc}\usepackage[utf8]{inputenc}
\usepackage{babel, xspace, tocbasic}
\usepackage{pdfescape}

\makeatletter
\newcounter{Anl}
\newcommand*{\MakeAnlLabel}[2]{%
  \begingroup
    \set@display@protect
    \let\IeC\@firstofone
    \EdefEscapeHex\@temp{#2}%
  \edef\@temp{%
    \endgroup
    \def\noexpand#1{\@temp}%
  }%
  \@temp
}
\newcommand*{\Anlv}[1]{%
  \refstepcounter{Anl}%
  \textit{attachment \theAnl}%
  \addtocontents{anl}{%
    \mdseries Attachment \theAnl: #1%
  }%
  \MakeAnlLabel\tmp{#1}%
  \label{ANL:\tmp}%
}
\newcommand*{\anlref}[1]{%
  \MakeAnlLabel\tmp{#1}%
  \ref{ANL:\tmp}%
}
\DeclareNewTOC[%
type=anlverz,%
types=anlverzes,%
nonfloat,%
name=Anlagen,%
listname={Attachments:}%
]{anl}
\makeatother

\begin{document}

Above you see a new command which I use to add attachments (example
for a reference: \anlref{Map of Özß, map scale 1:2500}) to my texts.
Often I need to refer to an attachment, e.g.\@ see the Map of Özß
(\Anlv{Map of Özß, map scale 1:2500}).

\listoftoc{anl}

\end{document}

结果

相关内容