这是对上一个问题的扩展:按颜色字母顺序显示 itemize 中的项目。我希望在带有索引和脚注的排序项目中使用颜色。薛定谔的猫提供的解决方案非常有效,但当我有多个以相同字母开头的项目时,似乎会出现以下错误:
! TeX 容量超出,抱歉 [输入堆栈大小=5000]。 \reserved@a ->\def \reserved@a *{@sdtlgetdatatype }\reserved@a l.48 \end{sortedlist}
以下是 MWE:
\documentclass{article}
\usepackage{xcolor}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[2][\empty]{%
\DTLnewrow{list}% Create a new entry
\DTLnewdbentry{list}{description}{#2}% Add entry as description
\DTLnewdbentry{list}{font}{#1}% Add entry as font
}
\newenvironment{sortedlist}{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
\DTLsort{description}{list}% Sort list
\begin{itemize}%
\DTLforeach*{list}{\theDesc=description,\myFont=font}{%
\item {\myFont\theDesc}}% Print each item
\end{itemize}%
}
\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing
\usepackage{xcolor} % Required for specifying colors by name
% This is necessary to allow colorizing the mark for the footnotes, from https://tex.stackexchange.com/questions/26693/change-the-color-of-footnote-marker-in-latex
\definecolor{bondiblue}{rgb}{0.0, 0.58, 0.71} %use this color for footnotes.
\makeatletter
\renewcommand\@makefnmark{\hbox{\@textsuperscript{\normalfont\color{bondiblue}\@thefnmark}}}
\makeatother
\makeatletter
\newcommand\footnoteref[1]{\protected@xdef\@thefnmark{\ref{#1}}\@footnotemark}
\makeatother
\begin{document}
Sorted:
\begin{sortedlist}
\sortitem{ISDYNSTP and such:}
\sortitem[\protect\color{red}]{ZVAR is good\footnote{about zvar}\index{Zvar}}
\sortitem{ISCDCA:}
\sortitem{PFT}
\sortitem[\protect\color{orange}]{MVAR is okay too\footnote{about mvar}\index{mvar}}
\sortitem[\protect\color{blue}]{MCAR is fine\footnote{about mcar}\index{mcar}}
\sortitem{IS2TL}
\end{sortedlist}
\printindex % Output the index
\end{document}
如果我删除“MVAR”或“MCAR”行,上述代码就会起作用,但我不能同时删除两者。这似乎与脚注和索引有关。
如果我同时有“MVAR”和“MCAR”,但没有脚注或索引,它就可以正常工作。但如果我有“MVAR”或“MCAR”的索引或脚注,它就不起作用。有没有办法解决这个问题,让两者都出现?我希望能够有任意数量的项目(带有脚注和/或索引)以相同的字母开头。任何帮助都将不胜感激,谢谢!
答案1
这可能是我的错,也许我应该更多地强调在这项业务中的作用\protect
:您需要对\protect
正在使用的宏进行修改,以防止过度扩展。我对命令进行了此\color
操作,但这也适用于\footnote
和\index
命令。对您添加的宏执行此操作会产生(大概)预期的结果。
\documentclass{article}
\usepackage{xcolor}
\usepackage{datatool}% http://ctan.org/pkg/datatool
\newcommand{\sortitem}[2][\empty]{%
\DTLnewrow{list}% Create a new entry
\DTLnewdbentry{list}{description}{#2}% Add entry as description
\DTLnewdbentry{list}{font}{#1}% Add entry as font
}
\newenvironment{sortedlist}{%
\DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}% Create new/discard old list
}{%
\DTLsort{description}{list}% Sort list
\begin{itemize}%
\DTLforeach*{list}{\theDesc=description,\myFont=font}{%
\item {\myFont\theDesc}}% Print each item
\end{itemize}%
}
\usepackage{makeidx} % Required to make an index
\makeindex % Tells LaTeX to create the files required for indexing
\usepackage{xcolor} % Required for specifying colors by name
% This is necessary to allow colorizing the mark for the footnotes, from https://tex.stackexchange.com/questions/26693/change-the-color-of-footnote-marker-in-latex
\definecolor{bondiblue}{rgb}{0.0, 0.58, 0.71} %use this color for footnotes.
\makeatletter
\renewcommand\@makefnmark{\hbox{\@textsuperscript{\normalfont\color{bondiblue}\@thefnmark}}}
\makeatother
\makeatletter
\newcommand\footnoteref[1]{\protected@xdef\@thefnmark{\ref{#1}}\@footnotemark}
\makeatother
\begin{document}
Sorted:
\begin{sortedlist}
\sortitem{ISDYNSTP and such:}
\sortitem[\protect\color{red}]{ZVAR is good\protect\footnote{about zvar}\protect\index{Zvar}}
\sortitem{ISCDCA:}
\sortitem{PFT}
\sortitem[\protect\color{orange}]{MVAR is okay too\protect\footnote{about mvar}\protect\index{mvar}}
\sortitem[\protect\color{blue}]{MCAR is fine\protect\footnote{about mcar}\protect\index{mcar}}
\sortitem{IS2TL}
\end{sortedlist}
\printindex % Output the index
\end{document}
运行时会生成索引makeindex
。