使用 catcode 自动索引复杂公式

使用 catcode 自动索引复杂公式

原始帖子

以下是我正在开发的程序的最低限度的工作代码。我的问题是,当我使用更复杂的代码时,它不会在最后自动索引。

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{imakeidx} %allows for multiple indexes
\usepackage{xcolor}

\catcode`"=\active
\def"#1" {\textcolor{white}{#1}} %catcode to color words white, as to hide it on the the compile format

\catcode`@=\active
\def@[#1][#2][#3]@ {#2\index[#1]{$\square$ "#3" #2}}  %catcode for FORMULAS that are sent to specific indexes as needed, here I use the catcode for "" in order to hide the letter that will allow for the index to be rearranged. so #3 will denote the order of the index, however it will be invisible to the naked eye on white paper so that the formula indexes are easier to read

%making indexes as needed
%  \makeindex[name=NICKNAME, title={INDEX_TITLE},columns=1, intoc]

\makeindex[name=PV, title={Present Value},columns=1, intoc]

\usepackage{lipsum} %creating filler text for demonstration/test purposes

\begin{document}

@[PV][$PV$=summation $\frac{C}{(1+r)^n}$][c]@
\index[PV]{$\square  PV = \sum \frac{C}{(1+r)^n}$}

@[PV][$PV = \sum \frac{C}{(1+r)^n}$][b]@


\indexprologue{Present Value is the discounted value, is the value of an expected income stream determined as of the date of valuation}
\printindex[PV]  

\end{document}

当我按常规方式将某些内容添加到索引中时,复杂的公式确实会以类似的形式显示。这仍然不理想。有没有办法更好地定义 \catcode,以便我可以将更复杂的公式输入到索引中?

更新代码

以下是根据@barbara beeton 信息更新的代码

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{imakeidx} %allows for multiple indexes
%\usepackage{xcolor} no longer needed

%\catcode`"=\active
%\def"#1" {\textcolor{blue}{#1}}  no longer need this as redefined the following catcode

\catcode`"=\active %changed symbol as @ is a seperator in makeidx
\def"[#1][#2][#3]" {#2\index[#1]{#3@$\square$ #2}}  %catcode for FORMULAS that are sent to specific indexes as needed, #3 is used to take advantage of the fact that indexes are alphabetical to order the formulas in an organized manor. Ultimately this /catcode is used to print the #2 in the main text AND index #2 into the appropriate index, #1. 


%making indexes as needed
%  \makeindex[name=NICKNAME, title={INDEX_TITLE},columns=1, intoc]
\makeindex[name=PV, title={Present Value},columns=1, intoc]

\usepackage{lipsum} %creating filler text for demonstration/test purposes

\begin{document}

"[PV][$PV$= summation $\frac{C}{(1+r)^n}$][c]" %appears in index and maintext
\lipsum[2]
"[PV][$PV=\frac{C}{(1+r)^n}$][a]" %appears in index and main text

"[PV][$PV=\sum\frac{C}{(1+r)^n}$][b]" %appears in maintext BUT NOT IN INDEX


%this is what i want to appear in the index using the catcode
%\index[PV]{$\square PV=\sum\frac{C}{(1+r)^n}$}


\indexprologue{Present Value is the discounted value, is the value of an expected income stream determined as of the date of valuation}
\printindex[PV]  

\end{document}

答案1

因此,从评论中,@barbara beeton 能够找到问题。我发布了一个答案,以便其他人能够找到并解决。

  • 原始代码中的语法错误:@已经是一个分隔符,\makeidx因此重新定义会\catcode导致问题,并且代码比需要的更复杂。解决方案:使用 " 作为 catcode 符号

此后,文件将会顺利编译而不会出现任何错误,但是索引中仍然缺少公式。

解决方案

  1. 首先要了解索引日志文件中正在解释的内容。.ilg文件将告诉您在编译索引的过程中是否存在任何错误以及是否有任何内容被忽略。如果有任何错误或任何条目被拒绝或忽略,请转到下一步
  2. .idx将显示正在读入索引的确切内容。因此,将其打开到任何文本文件阅读器(如记事本)中。这将向您显示您的输入如何被翻译并读入索引,这可以帮助您查明有问题的部分。在我的情况下,\sum被读为\DOTSB \sum@ \slimits@如果你的命令被转换成一些奇怪的命令,那么继续上一步
  3. \protect将阻止命令转换为其他命令。因此,在这种情况下,'$\sum\frac$ 现在是 '\protect\sum \frac',现在复杂公式出现在索引中,没有任何错误。这也可以通过查看.idx和文件来确认。.ilg

最终代码

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{latexsym}
\usepackage{imakeidx} %allows for multiple indexes


\catcode`"=\active 
\def"[#1][#2][#3]" {#2\index[#1]{#3@$\square$ #2}}  %catcode for FORMULAS that are sent to specific indexes (defined by #1) as needed. #3 is used to take advantage of the fact that indexes are alphabetical to order the formulas in an organized manor. Ultimately this /catcode is used to automatically print the #2 in the main text AND index #2 into the appropriate index at the same time 


%making indexes as needed
%  \makeindex[name=NICKNAME, title={INDEX_TITLE},columns=1, intoc]
\makeindex[name=PV, title={Present Value},columns=1, intoc]


\begin{document}


%"[PV][$PV=\sum \frac{C}{(1+r)^n}$][b]" %appeared in maintext BUT NOT IN INDEX

"[PV][$PV=\protect\sum \frac{C}{(1+r)^n}$][b]" %appears in both index and main text


\indexprologue{Present Value is the discounted value, is the value of an expected income stream determined as of the date of valuation}
\printindex[PV]  

\end{document}

相关内容