![参考旁注](https://linux22.com/image/376980/%E5%8F%82%E8%80%83%E6%97%81%E6%B3%A8.png)
我正在尝试使用旁注包裹。
当我引用第二个旁注时,文本是“旁注 1.1.2”,但对于第一个旁注,显示的是“旁注 1.1”。如何修复此错误?
这是一个紧凑的工作代码:
\documentclass[12pt]{book}
\usepackage{float}
\usepackage{caption}
\usepackage{sidenotes}
\usepackage[outer=7cm]{geometry}
\usepackage{chngcntr}
\counterwithin{sidenote}{section}
\makeatletter
\renewcommand{\thesidenote}{\@arabic{\numexpr\value{chapter}\relax}.\@arabic{\numexpr\value{section}\relax}.\@arabic{\numexpr\value{sidenote}+1\relax}}
\makeatother
\begin{document}
\chapter{Example}
\section{Cake}
This is an example.\sidenote{First sidenote.\label{first}} \\
This is second\sidenote{Second sidenote.\label{second}} \\
More text... \\
...
... \\
Then I refer to sidenote \ref{first} and then to sidenote \ref{second}.
\end{document}
代码编译,但最后一句话出现如下
然后我参考边注 1.1,然后参考边注 1.1.2。
答案1
问题在于,该sidenotes
包sidenote
在末尾增加了计数器,因此只有在旁注结束后才会设置引用。可以通过在设置旁注之前更改\sidenotetext
为执行以下操作来解决这个问题:\refstepcounter
\makeatletter
\ExplSyntaxOn
\RenewDocumentCommand \sidenotetext { o o +m }
{
\IfNoValueOrEmptyTF{#1}
{
\refstepcounter{sidenote}
\@sidenotes@placemarginal{#2}{\textsuperscript{\thesidenote}{}~#3}
}
{
\@sidenotes@placemarginal{#2}{\textsuperscript{#1}~#3}
}
}
\ExplSyntaxOff
\makeatother
然后\sidenote
也必须进行更改以避免在文本中打印错误的数字:在原始代码中首先设置标记(使用旧的计数器值),我们要先添加注释(并增加计数器):
\RenewDocumentCommand \sidenote { o o +m }
{
\@sidenotes@multichecker
\sidenotetext[#1][#2]{#3}
\sidenotemark[#1]
\@sidenotes@multimarker
}
这也取代了你的\renewcommand\thesidenote
,所以我们得到
\documentclass[12pt]{book}
\usepackage{float}
\usepackage{caption}
\usepackage{sidenotes}
\usepackage[outer=7cm]{geometry}
\usepackage{chngcntr}
\counterwithin{sidenote}{section}
\makeatletter
\ExplSyntaxOn
\RenewDocumentCommand \sidenote { o o +m }
{
\@sidenotes@multichecker
\sidenotetext[#1][#2]{#3}
\sidenotemark[#1]
\@sidenotes@multimarker
}
\RenewDocumentCommand \sidenotetext { o o +m }
{
\IfNoValueOrEmptyTF{#1}
{
\refstepcounter{sidenote}
\@sidenotes@placemarginal{#2}{\textsuperscript{\thesidenote}{}~#3}
}
{
\@sidenotes@placemarginal{#2}{\textsuperscript{#1}~#3}
}
}
\ExplSyntaxOff
\makeatother
\begin{document}
\chapter{Example}
\section{Cake}
This is an example.\sidenote{First sidenote.\label{first}} \\
This is second\sidenote{Second sidenote.\label{second}} \\
More text... \\
...
... \\
Then I refer to sidenote \ref{first} and then to sidenote \ref{second}.
\end{document}