当 hyperref 处于打开状态时更新 \thelstlisting 命令

当 hyperref 处于打开状态时更新 \thelstlisting 命令

我正在尝试将交叉引用的外观更改为附录中的列表,以便将其打印为\textsc{a}.1,而不仅仅是。当 hyperref 关闭时,A.1重新定义 可以正常工作。和的类似重新定义适用于 hyperref,但这个适用于。我可以使用 使其工作,但对于完整重新定义,我仍然需要,而这又不起作用。\renewcommand{\thelstlisting}{\MakeLowercase{\thechapter}.\arabic{lstlisting}}\thefigure\thetable\thelstlisting\lowercase\textsc\renewcommand{\thelstlisting}{\textsc{\lowercase{\thechapter}}.\arabic{lstlisting}}

自己检查一下:

\documentclass[english]{report}

% once you make everything work, try with
% \usepackage{microtype}


\usepackage[colorlinks=true]{hyperref}

\usepackage{listings}
\lstset{frame=single}


\begin{document}
% \MakeLowercase does not work here
% \lowercase does
\renewcommand{\thelstlisting}{\MakeLowercase{\thechapter}.\arabic{lstlisting}}
%  but what I really want is
% \renewcommand{\thelstlisting}{\textsc{\MakeLowercase{\thechapter}}.\arabic{lstlisting}}
%and that does not work again

% always OK
\renewcommand{\thetable}{\textsc{\lowercase{\thechapter}}.\arabic{table}}

\chapter{Title}

Some text with a cross-ref to two listings \ref{chapter_listing}
\ref{appendix_listing} and a table \ref{appendix_table} at the end.

Expected: 1.1 and \textsc{a}.1 (not \textsc{A}.1)

\vspace{6cm}


\begin{lstlisting}[caption={first caption},label={chapter_listing}]
a few lines
of
pseudocode will hopefully do
\end{lstlisting}


\appendix

\chapter{Appendix test}

\begin{lstlisting}[caption={second caption},label={appendix_listing}]
a few lines
of
pseudocode will hopefully do
\end{lstlisting}

\begin{table}
\caption{\label{appendix_table}table caption}
\begin{tabular}{|c|c|}
\hline 
first & second\tabularnewline
\hline 
\end{tabular}
\end{table}

\end{document}

问题出在哪里?

答案1

这里有一个选项:当你到达时重新定义\thelstlisting\thetable(和其他......):{\scshape\alph{chapter}}.\arabic{<cntr>}\appendix

在此处输入图片描述

\documentclass{report}

\usepackage[colorlinks=true]{hyperref}

\usepackage{listings}
\lstset{frame=single}

\renewcommand{\thetable}{{\scshape\thechapter}.\arabic{table}}
\AtBeginDocument{%
  \renewcommand{\thelstlisting}{{\scshape\thechapter}.\arabic{lstlisting}}%
}
\let\oldappendix\appendix
\renewcommand{\appendix}{%
  \oldappendix
  \renewcommand{\thelstlisting}{{\scshape\alph{chapter}}.\arabic{lstlisting}}
  \renewcommand{\thetable}{{\scshape\alph{chapter}}.\arabic{table}}%
}

\begin{document}

\chapter{Title}

Some text with a cross-ref to two Listings~\ref{chapter_listing}
and~\ref{appendix_listing} and a Table~\ref{appendix_table} at the end.

Expected: 1.1 and \textsc{a}.1 (not \textsc{A}.1)

\begin{lstlisting}[caption={first caption},label={chapter_listing}]
a few lines
of
pseudocode will hopefully do
\end{lstlisting}

\appendix
\chapter{Appendix test}

\begin{lstlisting}[caption={second caption},label={appendix_listing}]
a few lines
of
pseudocode will hopefully do
\end{lstlisting}

\begin{table}
  \caption{\label{appendix_table}table caption}
\end{table}

\end{document}

我已经用{\scshape ...}代替了\textsc

答案2

不可扩展的内容(\lowercase\textsc\MakeLowercase)不得出现在任何计数器值中,该计数器值用于锚点名称。包hyperref已经使用代替。但是它对于 失败了,因为它使用代替。\theH<counter>\the<counter>\theHstnumber\thelstlisting\theHlstlisting

使固定:

\documentclass[english]{report}

\usepackage{microtype}
\usepackage[colorlinks=true]{hyperref}

\usepackage{listings}
\lstset{frame=single}

\begin{document}
\renewcommand{\thelstlisting}{%
  \textsc{\MakeLowercase{\thechapter}}.\arabic{lstlisting}%
}

\makeatletter
\renewcommand*{\theHlstnumber}{%
  \ifx\lst@@caption\@empty
    \lst@neglisting
  \else
    \theHlstlisting % fixed: \theHlstlisting instead of \thelstlisting
  \fi
  .\thelstnumber
}
\makeatother

\renewcommand{\thetable}{\textsc{\MakeLowercase{\thechapter}}.\arabic{table}}

\chapter{Title}

Some text with a cross-ref to two listings \ref{chapter_listing}
\ref{appendix_listing} and a table \ref{appendix_table} at the end.

Expected: 1.1 and \textsc{a}.1 (not \textsc{A}.1)

\vspace{6cm}

\begin{lstlisting}[caption={first caption},label={chapter_listing}]
a few lines
of
pseudocode will hopefully do
\end{lstlisting}


\appendix

\chapter{Appendix test}

\begin{lstlisting}[caption={second caption},label={appendix_listing}]
a few lines
of
pseudocode will hopefully do
\end{lstlisting}

\begin{table}
\caption{\label{appendix_table}table caption}
\begin{tabular}{|c|c|}
\hline
first & second\tabularnewline
\hline
\end{tabular}
\end{table}

\end{document}

(省略图片,因为我认为参考文献中的小写字母在章节下方非常难看)。

相关内容