使用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導致的問題和比所需更複雜的代碼。解決方案:使用 " 作為目錄代碼符號

此後,該檔案將編譯而不會出現任何錯誤,但索引中仍會缺少公式。

解決方案

  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}

相關內容