显示目录、图片列表、表格列表等中的标签的“键”

显示目录、图片列表、表格列表等中的标签的“键”

在编辑大型文档时,各个编辑者负责处理各个章节,团队中的一些人经常需要参考labels其他keys章节。

用户 jfbu 整理出了一个不错的解决方案……代码在页面上。由于大型多章节多编辑器文档的情况总是有点复杂,因此 mwe 也有点复杂。我在 mwekeys.zip 文件中发布了一个:http://hep.ucsb.edu/mwe/。下面显示了显示键的三页(TOC、lof 和 lot)输出。

还有一个缺陷...与包一起使用时chapterbib,末尾有一个挂起`(./mwekeys.aux(./ONE.aux)(./TWO.aux))失控参数?!扫描 \@writefile 的使用时文件结束。 \par l.101 \end{document}

问`

这种挂起仅会发生在chapterbib

目录 洛夫 很多

答案1

最后更新:下面的 hacky 代码依赖于aux文件末尾不包含一行\@writefile(当涉及lotlof、 时toc)。因此,需要一些方法将例如 放在文件\relax的最末尾.aux

我在包的帮助下实现了这一点尽头,并且对于 OP 提供的示例文件来说,这个修改看起来足够强大。

我删除了代码的第一个版本,只留下了添加两处修改的代码(这里描述的和\KOMAoptions下面描述的)。


倒数第二次更新:还添加了loflot到 hack 处理的实体。似乎有效。请参阅答案底部的代码。


更新正如评论中所述,OP 使用scrbook文档类。下面的代码最初使用 article没有 的类进行测试chapter\chapter默认情况下,该命令(无论是book还是scrbook)发出命令以在图片列表和表格列表中添加垂直间距。这些命令最终出现在文件.aux(或文件)中,而下面的宏没有看到它\newlabel正在寻找的。

  1. 在 下KOMAscrbook可以使用以下解决方法。添加:

    \KOMAoptions{listof=nochaptergap}
    

    回到序言。

  2. 如果使用标准book类,那么最简单的方法就是修补(条件是布尔值\ifHNNdraft)内核宏\@chapter,从中删除\addtocontents {lof}{\protect \addvspace {10\p@ }}\addtocontents {lot}{\protect \addvspace {10\p@ }}。由于肯定有许多软件包修补\chapter/\@chapter命令,这可能会证明有些脆弱,因此我不再讨论这个问题。允许KOMA使用上面给出的更简单的解决方案。下面的宏不会导致错误,但它对后面的标签无效。它对 和\chapter正常工作。\part\section

评论根据过去的经验,我知道biblatex有一种非常顽固的倾向,即在文件的行间添加命令.toc,因此我怀疑这可能会危及此处的黑客行为。幸运的是,在文件级别aux,这是在\@writefile行为在此处修补的行之前完成的,因此我的宏确实\newlabel.aux文件中找到了下一个,并且可能与之兼容biblatex(至少我的单次测试如此)。

最后说明:下面的代码已经过测试hyperref并且兼容。


过时的:这是一个 hack,我根本没有测试过(你没有提供我可以复制粘贴的 mwe)。也许它会在第一次机会时被破坏。但给出了这个想法。


原始代码已被删除。这是最终代码,使用尽头包和\KOMAoptions命令来解决各种问题。

\documentclass {scrbook}
\usepackage{hyperref}% for testing compatibility
\usepackage{varioref}
\usepackage{chapterbib}% for testing compatibility

\usepackage{mwe}

% to put something at very end of main aux file
\usepackage{atveryend}% also loaded by hyperref
\makeatletter
    \AfterLastShipout{\immediate\write\@mainaux{\relax}}%
\makeatother

% boolean to activate or not insertion of label keys in toc, lof, lot:
\newif\ifHNNdraft
\HNNdrafttrue
% suppress key insertions in toc, lof, lot:
% \HNNdraftfalse

\makeatletter

\let\original@writefile\@writefile

\def\@writefile #1#2#3{\def\HNN@fileext{#1}%
     \let\HNN@next\original@writefile
     \in@{#1}{toc,lof,lot}%
     \ifin@
       \ifx\newlabel#3%
          \def\HNN@next ##1##2\newlabel
         {\futurelet\HNN@tmp\HNN@writefile #2\empty\HNN@writefile}%
       \fi
     \fi
     \HNN@next {#1}{#2}#3% fingers extra crossed
}%
\def\HNN@writefile 
{%
    \ifx\HNN@tmp\contentsline\expandafter\HNN@writefile@a\else
                             \expandafter\HNN@writefile@abort
    \fi
}%
\def\HNN@writefile@a \contentsline #1#2#3\HNN@writefile #4%
{%
    \original@writefile{\HNN@fileext}{\contentsline{#1}{#2\HNN@showkey {#4}}#3}%
    \newlabel {#4}%
}%
\def\HNN@writefile@abort #1\HNN@writefile
{%
    \original@writefile{\HNN@fileext}{#1}\newlabel
}%
\protected\def\HNN@showkey #1%
         {\ifHNNdraft\space\space \textit{key:} \texttt{#1}\fi}

\ifHNNdraft
   \KOMAoptions{listof=nochaptergap}
\fi
\makeatother

\begin{document}
\tableofcontents

\listoffigures

\listoftables

\thispagestyle{empty}

\part{PART WITH LABEL}\label{hello}

\chapter{CHAPONE}\label{chap1}

\begin{figure}
\includegraphics[width=.48\linewidth]{example-image-a}\hfill
\includegraphics[width=.48\linewidth]{example-image-b}
\caption{FIRST MWE to demonstrate how to place to images side-by-side}
\label{fig:BU}
\end{figure}

\section{foo}\label{foo}

Let me check \autoref{bar}.
Let me check \autoref{bob}.

\section{bar}\label{bar}

Let me check \autoref{foo}.
\begin{figure}
\includegraphics[width=.48\linewidth]{example-image-a}\hfill
\includegraphics[width=.48\linewidth]{example-image-b}
\caption{SECOND MWE to demonstrate how to place to images side-by-side}
\label{fig:JF}
\end{figure}

\section{with no label}
\subsection{with no label}

\part{PART WITHOUT LABEL}

\chapter{CHAPONE OF PART TWO}\label{chapII1}
\section{without label}
\begin{table}
  \centering
  \begin{tabular}{ccc}
    mountain&sea&lake
  \end{tabular}
  \caption{Sample table}\label{JF}
\end{table}
\section{bob}\label{bob}
\begin{table}
  \centering
  \begin{tabular}{ccc}
    apple&banana&strawberry
  \end{tabular}
  \caption{Sample table II}\label{BU}
\end{table}
\section{bib}
Let me check \autoref{foo}.

\end{document}

下图展示了最初发布的测试文件。结果与上述代码类似,其中的键也位于 和LOTLOF

引用

答案2

像这样?这会挂接到ToC并向\label其添加toc条目,同时还会提供命令。.lablist of labels

cleveref目前可能不支持

draft简短说明:目前它不采取该选项。

\documentclass{book}




\makeatletter
\newcommand{\listoflabels}{%
  \section*{List of labels}
  \@starttoc{lab}
}
\makeatother



\usepackage{xpatch}

\xapptocmd{\label}{\addcontentsline{lab}{section}{\texttt{#1}}}{}

\begin{document}
\tableofcontents


\listoflabels

\chapter{First}\label{firstchapter}

\section{First}\label{first}


\begin{equation}
  E=mc^2 \label{firstequation}
\end{equation}


\end{document}

在此处输入图片描述

更新使用labelsdocumentclass 选项:

\documentclass[labels]{book}

\newif\iflabeloptionused

\DeclareOption{labels}{\labeloptionusedtrue}
\ProcessOptions*



\usepackage{blindtext}


\makeatletter
\newcommand{\listoflabels}{%
  \iflabeloptionused
  \section*{List of labels}
  \@starttoc{lab}
  \fi
}
\makeatother



\usepackage{xpatch}

\xapptocmd{\label}{\addcontentsline{lab}{section}{\texttt{#1}}}{}

\begin{document}
\tableofcontents


\listoflabels

\chapter{First}\label{firstchapter}

\section{First}\label{first}

\blindtext[15]
\begin{equation}
  E=mc^2 \label{firstequation}
\end{equation}


\end{document}

相关内容