是否可以使用包裝中的數字lineno
作為計數器imakeidx
?正如下面的 MWE 所示,我嘗試將xpatch
索引的計數器更改為thelinenumber
,但沒有成功:
\documentclass{article}
\usepackage{lineno}
\usepackage{imakeidx}
\usepackage{xpatch}
\makeindex
\makeatletter
\makeatletter
\patchcmd{\@wrindex}{\thepage}{\thelinenumber}{}{}
\makeatother
\begin{document}
\linenumbers
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed dapibus
facilisis magna, id sodales ante convallis vitae. In a porttitor nibh,
eget vehicula dui\index{test}. Integer feugiat dui nec neque porttitor, non eleifend
erat lobortis. Sed varius malesuada nisi, id vulputate ante commodo vitae.
Nam non felis at ante mollis commodo eget porttitor ex. Donec in quam diam.
Maecenas fringilla, turpis a egestas pulvinar, massa lacus molestie enim,
vel vulputate lorem ante ac velit. Nam ex nulla, venenatis et tortor at,
efficitur fringilla ante.
\clearpage \printindex
\end{document}
答案1
您不能直接使用\thelinenumber
,因為這將引用當段落已被拆分為行時計算的行號。
使用你的補丁,你得到的數字是 1,因為\thelinenumber
在錯誤的時間擴展了;但\noexpand\thelinenumber
你會得到 8,因為這是處理該段落時的值(它有七行)。
你需要使用該\linelabel
機制。
\documentclass{article}
\usepackage{lineno,refcount}
\usepackage{imakeidx}
\usepackage{xpatch}
\makeindex
\makeatletter
\newcommand{\lineindex}[1]{\linelabel{#1}\index{#1}}
\patchcmd{\@wrindex}{\thepage}{\getrefnumber{#2}}{}{}
\makeatother
\begin{document}
\linenumbers
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed dapibus
facilisis magna, id sodales ante convallis vitae. In a porttitor nibh,
eget vehicula dui\lineindex{dui}. Integer feugiat dui nec neque porttitor, non eleifend
erat lobortis. Sed varius malesuada nisi, id vulputate ante commodo vitae.
Nam non felis at ante mollis commodo eget porttitor ex. Donec in quam diam.
Maecenas fringilla, turpis a egestas pulvinar, massa lacus molestie enim\lineindex{enim},
vel vulputate lorem ante ac velit. Nam ex nulla, venenatis et tortor at,
efficitur fringilla ante.
\printindex
\end{document}
這需要兩次運行,因為它使用\ref
(實際上是可擴展版本\getrefnumber
)。