努力将 lualatex 添加到索引、排序和打印

努力将 lualatex 添加到索引、排序和打印

相关lualatex:如何添加到字符串或表格。我正在尝试将关键字(带有页码)添加到内存数组中,并在章节末尾打印已排序的顺序索引。应该很简单,但事实并非如此。

我完全被我遇到的各种奇怪的错误搞糊涂了。我花了一些时间把它缩减到我希望有一个例子可以展示所有可能出错的地方:

\documentclass[12pt]{memoir}

\usepackage{xifthen}  %% \isempty and \ifthenelse

\newif\ifshowproblem
\showproblemfalse

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% keyword-index.sty

%% use pageref if available; otherwise, say 'na'

\ifshowproblem
  \makeatletter
  \def\chkpageref#1{\@ifundefined{r@#1}{na}{\pageref{#1}}}
  \makeatother
\else
  \def\chkpageref#1{(not now)}
\fi

%%%%%%%%%%%%%%%%

\def\addtosort#1{%
  \directlua{%
    tbl=tbl or {}%
    table.insert(tbl,"\luaescapestring{#1}")%
  }%
}

\providecommand{\keyword}{}
%% alternative interface with optional refindex
%% needs to use \MakeLowercase later, and replace chkpageref with \chkpageref
\renewcommand{\keyword}[2][]{%
\ifshowproblem
  \ifthenelse{\isempty{#1}}{%
    \addtosort{\textit{#2}, pg.~\chkpageref{KW:#2}}\label{KW:#2}\typeout{KWF1: #2}}{%
    \addtosort{\textit{#2}, pg.~\chkpageref{KW:#1}}\label{KW:#1}\typeout{KWF2: #1}}%
  \else
  \ifthenelse{\isempty{#1}}{%
    \addtosort{textit{#2}, pg.~chkpageref{KW:#2}}\label{KW:#2}\typeout{KWF1: #2}}{%
    \addtosort{textit{#2}, pg.~chkpageref{KW:#1}}\label{KW:#1}\typeout{KWF2: #1}}%
  \fi
  \textbf{#2}%
}

%%%%%%%%%%%%%%%%

\def\printsorted{%
  \directlua{%
    table.sort(tbl)%
    for i,n in ipairs(tbl) do tex.write(n .. ';\hspace{1em} ') end%
  }}


\newcommand{\placekeywordshere}{%
  \typeout{KW: NOW SHOWING}
  \bigskip
  \noindent\rule{\textwidth}{3pt}
  \bigskip
  \noindent\textbf{Keywords: }
  \ifshowproblem
    \printsorted
  \else
    (printsorted is not shown, because showproblem is false.)
  \fi
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\chapter{Introduction}

\section{S1}

\keyword{Foxtrott} is on page 1.

\clearpage

\section{S2}

\keyword{BRAVO} and others are on page 2.

\keyword[xind]{x-Ray \& Co to the end}

\keyword{Alpha to the front}.

\section{EOC Material}

\placekeywordshere

\section{EOC WANTED}

\bigskip
\noindent\rule{\textwidth}{3pt}
\bigskip
\noindent\textbf{Keywords: }
alpha to the front, pg.~2; \hspace{1em} bravo, pg.~2; \hspace{1em} foxtrott, pg.~1; \hspace{1em} x-ray \& co to the end, pg.~2.


\end{document}

打开开关showproblem,所有可能出错的东西似乎都出错了。我认为我正在破坏 tex 和 lua 的东西,这使得跟踪变得困难。抱歉打扰了这么多。非常感谢您的建议。

答案1

如果最后一个关键字和打印点之间有分页符,则可以跳过辅助文件并需要两次运行乳胶。

在此处输入图片描述

\documentclass[12pt]{memoir}


%%%%%%%%%%%%%%%%

\def\addtosort#1{%
  \latelua{%
    tbl=tbl or {}%
    table.insert(tbl,"{\luaescapestring{#1}}{\thepage}")%
  }%
}

\providecommand{\keyword}{}
%% alternative interface with optional refindex
%% needs to use \MakeLowercase later, and replace chkpageref with \chkpageref
\renewcommand{\keyword}[1]{#1 \addtosort{#1}}


%%%%%%%%%%%%%%%%

\def\printsorted{%
  \directlua{%
    table.sort(tbl)%
    for i,n in ipairs(tbl) do
    tex.print("\string\\printkw" .. n)
     end%
  }}

\newcommand\printkw[2]{\textit{#1} pg #2;}

\newcommand{\placekeywordshere}{%
  \typeout{KW: NOW SHOWING}
  \bigskip
  \noindent\rule{\textwidth}{3pt}
  \bigskip
  \section*{Keywords: }
    \printsorted
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\chapter{Introduction}

\section{S1}

\keyword{Foxtrott} is on page 1.

\clearpage

\section{S2}

\keyword{BRAVO} and others are on page 2.

\keyword{x-Ray \& Co to the end}

\keyword{Alpha to the front}.

\clearpage
\section{EOC Material}

\placekeywordshere

\section{EOC WANTED}

\bigskip
\noindent\rule{\textwidth}{3pt}
\bigskip
\section*{Keywords: }
alpha to the front, pg.~2; \hspace{1em} bravo, pg.~2; \hspace{1em} foxtrott, pg.~1; \hspace{1em} x-ray \& co to the end, pg.~2.


\end{document}

按小写字母排序版本

在此处输入图片描述

\documentclass[12pt]{memoir}


%%%%%%%%%%%%%%%%

\def\addtosort#1{%
  \latelua{%
    tbl=tbl or {}%
    table.insert(tbl,"{" .. string.lower("\luaescapestring{#1}") .. "}{\luaescapestring{#1}}{\thepage}")%
  }%
}

\providecommand{\keyword}{}
%% alternative interface with optional refindex
%% needs to use \MakeLowercase later, and replace chkpageref with \chkpageref
\renewcommand{\keyword}[1]{#1 \addtosort{#1}}


%%%%%%%%%%%%%%%%

\def\printsorted{%
  \directlua{%
    table.sort(tbl)%
    for i,n in ipairs(tbl) do
    tex.print("\string\\printkw" .. n)
     end%
  }}

\newcommand\printkw[3]{\par\noindent\textit{#2} pg #3;\par}% discard lowercase #1 used for sort

\newcommand{\placekeywordshere}{%
  \typeout{KW: NOW SHOWING}
  \bigskip
  \noindent\rule{\textwidth}{3pt}
  \bigskip
  \section*{Keywords: }
    \printsorted
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\chapter{Introduction}

\section{S1}

\keyword{Foxtrott} is on page 1.

\clearpage

\section{S2}

\keyword{BRAVO} and others are on page 2.

\keyword{x-Ray \& Co to the end}

\keyword{Alpha to the front}.
\keyword{alpha lower case to the front}.

\clearpage
\section{EOC Material}

\placekeywordshere

\section{EOC WANTED}

\bigskip
\noindent\rule{\textwidth}{3pt}
\bigskip
\section*{Keywords: }
alpha to the front, pg.~2; \hspace{1em} bravo, pg.~2; \hspace{1em} foxtrott, pg.~1; \hspace{1em} x-ray \& co to the end, pg.~2.


\end{document}

相关内容