LaTeX中\ref显示的计数器数值与其实际数值(点击链接后对应位置显示的数值)不对应是为什么?

发布于 2022-09-11 16:55:53

选自:https://www.zhihu.com/question/552513350/answer/2667824611
我在使用网上找到的一个模板的时候出现了这样一个问题: 比如说我给自己的某一个定理设置的计数器是按照section计数的, 也就是说第一章第二节的第三个定理应该计数器的数值显示为1.2.3, 但是我在用ref实际援引这个计数器数值的时候却显示为1.2.1, 原因是这个定理我放在了第一章第二节第一小节(subsection), 在这一小节它是第一个定理. 所以最终的结果是我的定理环境编译出来显示的thetheorem的结果(1.2.3)和用ref{label}显示的结果(1.2.1)对不上. 这可能是因为设置了什么导致的?

具体地话, 是这样的:
image.png

这是某一个定理, 它对应的源码是:

\begin{theorem}[环的扩张定理]\label{thm:Ring2 3.3}
设$\hat S,R$是环, $\hat S\cap R=\varnothing$且存在一个环的单同态$\hat\phi:\hat S\hookrightarrow R$, 则存在环$S$和环同构$\phi:S\simeq R$使得$S$是$\hat S$的环扩张, 且$\phi|_{\hat S}=\hat\phi$. 
\end{theorem}

但是我在使用\ref{thm:Ring2 3.3}这个指令的时候显示的效果确是这样的:
image.png

变成了1.2.3

查看更多

关注者
0
被浏览
1.1k
2 个回答
LaTeXStudio
LaTeXStudio 2022-09-11
这家伙很懒,什么也没写!

作者:慕子

链接:https://www.zhihu.com/question/552513350/answer/2667824611

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

\newtcolorbox{wwtheorem}[3][]{...} 里的

  • step and label={tcbthm}{#3}, 改成 step=tcbthm, IfEmptyF={#3}{label={#3}},
  • 注意,键 IfEmptyFtcolorbox v5.1.0 (2022-06-22) 新加入的
  • 两处 {\bf \Theorem\ \thetcbthm.~#2} 改成 {\bf \Theorem\ \thetcbthm.\ifstrempty{#2}{}{~#2}}

[theorem](https://www.zhihu.com/search?q=theorem&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A2667824611%7D) 环境的用法

  • 无标题、无标签 \begin{theorem} ... \end{theorem}
  • 有标题、无标签 \begin{theorem}[<title>] ... \end{theorem}
  • 无标题、有标签 \begin{theorem}[][<label>] ... \end{theorem}
  • 有标题、有标签 \begin{theorem}[<title>][<label>] ... \end{theorem}

辅助的修改(不一定全),

  • \newcommand{\thmref}[1]{...} 里,把 \ref{#1} 改成 \ref*{#1},避免超链接嵌套
  • \begin{theorem}{理想的判定定理} 改成 \begin{theorem}[理想的判定定理], 可能是输入错误

其他建议(一定不全)

  • 补充例子时,建议优先编辑问题,而不是发一个回答。我不清楚是不是有限制了不让用户编辑自己提的非热门问题……
  • 不要用 \bf 了(可能要反馈给「模板」作者,下同)
  • \thmref 的定义里,\!\,\, 加加减减,可以考虑直接手动写了
  • 注意空格,比如 theorem 环境定义里,{\end{wwtheorem} }
  • \usepackage[most]{tcolorbox} 后为什么还要 \tcbuselibrary{breakable, skins,theorems}
  • hyperref 加载太早了,当心出问题。

应用上述所有修改(其他建议部分除外),包括以上述介绍的方式为 theorem 环境添加可选标签后,得到的结果

image.png

完整例子

% !TeX TS-program = xelatex
\documentclass[twoside]{book}

\usepackage{amssymb}
\usepackage[dvipsnames,svgnames,x11names,table]{xcolor}
\usepackage{ninecolors}
\usepackage{amsmath,amsthm}
\usepackage[colorlinks,linkcolor=red, breaklinks]{hyperref}
\usepackage[most]{tcolorbox}
\tcbuselibrary{breakable, skins,theorems}
\usepackage{tikz}
\usepackage{tkz-fct}
\usetikzlibrary{calc,spy,intersections,fadings,patterns,shapes.arrows,shapes.symbols,tikzmark,shapes.geometric,shadows, shadings,backgrounds,positioning,fit}
\usepackage{graphicx,tabularx}

\definecolor{lightergray}{gray}{0.99}

\definecolor{Rhodamine}{RGB}{239,84,159}

\usepackage{physics}
\usepackage{ctex}

\newcounter{tcbthm}
\numberwithin{tcbthm}{section}
\def\tcbthmautorefname{\bf 定理}
\newcommand{\Theorem}{定理}

\newtcolorbox{wwtheorem}[3][]{
        arc=0mm,breakable,enhanced,colback=lightergray,boxrule=0pt,top=8mm,drop fuzzy shadow=black!20!white,
        fontupper=\upshape, 
    step=tcbthm,
    IfEmptyF={#3}{label={#3}}, % require tcolorbox 2022-05-25 or newer
%    step and label={tcbthm}{#3},
        overlay unbroken = {
            %标题%更改有效
            \node[rectangle, %opacity=.3,
    text=white, drop shadow={opacity=.3, shadow xshift=0.1cm},
    inner sep=1.5mm,fill=purple5,
    anchor=west,inner xsep=7mm,
    font=\normalsize] at ([xshift=0cm,yshift=-3.mm]frame.north west)%
    {\bf   \Theorem\ \thetcbthm.\ifstrempty{#2}{}{~#2}};
            \fill[color=purple5,drop shadow={opacity=0.3,shadow xshift=.3pt}] ([yshift=5pt,xshift=-5pt]frame.north west) -- ([xshift=1cm,yshift=5pt]frame.north west)-- ([xshift=-5pt,yshift=-1cm]frame.north west)--cycle;
            \fill[color=black!60] ([xshift=-5pt,yshift=-1cm]frame.north west)--([xshift=0cm,yshift=-0.8cm]frame.north west)-- ([xshift=0cm,yshift=-1.2cm]frame.north west)--cycle;
            \fill[black!60] ([xshift=1cm,yshift=5pt]frame.north west)-- ([xshift=0.8cm,yshift=0cm]frame.north west)--([xshift=1.2cm,yshift=0cm]frame.north west)--cycle;
            %%============================%% End
            \fill[purple5,drop shadow={opacity=0.3,shadow xshift=-0.008\linewidth,shadow yshift=0.0016\linewidth}] ([xshift=5pt,yshift=-5pt]frame.south east) -- ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) -- ([xshift=5pt,yshift=0.05\linewidth]frame.south east)--cycle;
            \fill[black!60] ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) -- ([xshift=-0.04\linewidth,yshift=0pt]frame.south east)-- ([xshift=-0.06\linewidth,yshift=0pt]frame.south east)--cycle;
            \fill[black!60] ([xshift=5pt,yshift=0.05\linewidth]frame.south east)-- ([yshift=0.04\linewidth]frame.south east) -- ([yshift=0.06\linewidth]frame.south east)--cycle;
        },%上面是实际控制代码,下面的代码仅在跨页分割时生效
        overlay first = {%跨页时生效
            % \draw[color=gray,  line width=0.2pt] (frame.north west)--([xshift=0pt]frame.north east);
            %标题 %跨页时生效
            \node[rectangle, %opacity=.3,
    text=white, drop shadow={opacity=.3, shadow xshift=0.1cm},
    inner sep=1.5mm,fill=purple5,
    anchor=west,inner xsep=7mm,
    font=\normalsize] at ([xshift=0cm,yshift=-3.mm]frame.north west)%
    {\bf   \Theorem\ \thetcbthm.\ifstrempty{#2}{}{~#2}};
            \fill[color=purple5,drop shadow={opacity=0.3,shadow xshift=.3pt}] ([yshift=5pt,xshift=-5pt]frame.north west) -- ([xshift=1cm,yshift=5pt]frame.north west)-- ([xshift=-5pt,yshift=-1cm]frame.north west)--cycle;
            \fill[color=black!60] ([xshift=-5pt,yshift=-1cm]frame.north west)--([xshift=0cm,yshift=-0.8cm]frame.north west)-- ([xshift=0cm,yshift=-1.2cm]frame.north west)--cycle;
            \fill[black!60] ([xshift=1cm,yshift=5pt]frame.north west)-- ([xshift=0.8cm,yshift=0cm]frame.north west)--([xshift=1.2cm,yshift=0cm]frame.north west)--cycle;
        },%保持边缘的变化
        overlay last = {%跨页时生效
            % \draw[color=gray,  line width=0.2pt] (frame.south west)--([xshift=0pt]frame.south east);
            %%============================%% End
            \fill[purple5,drop shadow={opacity=0.3,shadow xshift=-0.008\linewidth,shadow yshift=0.0016\linewidth}] ([xshift=5pt,yshift=-5pt]frame.south east) -- ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) -- ([xshift=5pt,yshift=0.05\linewidth]frame.south east)--cycle;
            \fill[black!60] ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) -- ([xshift=-0.04\linewidth,yshift=0pt]frame.south east)-- ([xshift=-0.06\linewidth,yshift=0pt]frame.south east)--cycle;
            \fill[black!60] ([xshift=5pt,yshift=0.05\linewidth]frame.south east)-- ([yshift=0.04\linewidth]frame.south east) -- ([yshift=0.06\linewidth]frame.south east)--cycle;
        }
        #1
    }
\NewDocumentEnvironment{theorem}{O{} O{} O{}}{\smallskip\begin{wwtheorem}{#1}{#2}#3}{\end{wwtheorem}}

\newcommand{\lt}{<} 
\newcommand{\thmref}[1]{\hyperref[#1]{定理\!\,\,\ref*{#1}}}
\newcommand{\ideal}{\sphericalangle}

\begin{document}

\chapter{环论基础}\label{chap:Ring 1}

\section{环的相关定义以及分类}\label{sec:Ring 1}

\subsection{环的定义以及分类}\label{sec:Ring1.1}

\begin{theorem}[][1]
 $\mathbb{Z}_p$是域当且仅当$p$为质数. 
\end{theorem}

\begin{theorem}[][2]
 任何一个域的特征要么为0, 要么为质数.
\end{theorem}

\subsection{子环和环的理想}\label{sec:Ring1.2}


\begin{theorem}[子环的判定定理][3]
 设$R$是一个环, $S$是它的非空子集, 则$S\lt R$当且仅当对任意$a,b\in S$, 有(1) $a-b\in S$; (2) $ab\in S$. 
\end{theorem}

\begin{theorem}[理想的判定定理][4]
 设$R$是一个环, $I$是它的非空子集, 则$I$是$R$的左(右)理想当且仅当(1) 对于任意$a,b\in I$, 有$a-b\in I$; (2) 对于任意的$r\in R$和$i\in I$, 有$ri\in I$($ir\in I$). 
\end{theorem}

 

\begin{theorem}[][5]
 一个环中任意多个左(右)理想的交还是左(右)理想.
\end{theorem}


\section{环的同态定理与扩张}\label{sec:Ring2_1}


\subsection{商环}\label{sec:Ring2_1.1}

\begin{theorem}[][6]
在环$\expval{R,+,\cdot}$上定义等价关系$a\sim b:=a-b\in I$, 则这个等价关系关于环运算成同余关系当且仅当$I$是环$R$的理想.
\end{theorem}



\subsection{环的同态定理}\label{sec:Ring2_1.2}

\begin{theorem}[环同态的性质][7]
设$\phi:R\to S$是环同态, 如果$M\lt R$, $N\lt S$, 则

(1) $\phi[M]\lt\phi[R]\lt S$;

(2) 若$M\ideal R$, 则$\phi[M]\ideal\phi[R]$;

(3) $\phi^{-1}[N]\lt R$;

(4) 若$N\ideal S$, 则$\phi^{-1}[N]\ideal R$.
\end{theorem}



\begin{theorem}[环的同态定理][8]
设$f:R\to S$是环的满同态, $K=\ker f$是同态核, $I$是$R$的包含同态核的理想, 则$R/I\simeq S/f[I]$.
\end{theorem}


\begin{theorem}[环的第二同构定理][9]
如果$I$是环$R$的理想, $S$是环$R$的子环, 则(1) $S+I:=\{s+i\mid i\in I,s\in S\}$是环; (2) $S\cap I$是$S$的理想, $I$是$S+I$的理想; (3)且有环同构$S/(S\cap I)\simeq (S+I)/I$.
\end{theorem}

\begin{theorem}[环的第三同构定理][10]
如果$I$和$J$是环$R$的理想且$I\subseteq J$, 则$I$是$J$的理想且$(R/I)/(J/I)\simeq R/J$.
\end{theorem}

\subsection{环的扩张定理}\label{sec:Ring2_1.3}

\begin{theorem}[环的扩张定理][11]
设$\hat S,R$是环, $\hat S\cap R=\varnothing$且存在一个环的单同态$\hat\phi:\hat S\hookrightarrow R$, 则存在环$S$和环同构$\phi:S\simeq R$使得$S$是$\hat S$的环扩张, 且$\phi|_{\hat S}=\hat\phi$. 
\end{theorem}


\section{引用定理}

\begin{center}
\begin{tabular}{c|cc}
  \hline 
显示 & 真值 & 所在位置\\ 
\hline
\thmref{1}&定理1.1.1& subsection 1.1.1    \\ 
\thmref{2}&定理1.1.2& subsection 1.1.1    \\ 
\thmref{3}&定理1.1.3& subsection 1.1.2    \\ 
\thmref{4}&定理1.1.4& subsection 1.1.2    \\ 
\thmref{5}&定理1.1.5& subsection 1.1.2    \\ 
\thmref{6}&定理1.2.1& subsection 1.2.1    \\ 
\thmref{7}&定理1.2.2& subsection 1.2.2    \\ 
\thmref{8}&定理1.2.3& subsection 1.2.2    \\ 
\thmref{9}&定理1.2.4& subsection 1.2.2    \\ 
\thmref{10}&定理1.2.5& subsection 1.2.2    \\ 
\thmref{11}&定理1.2.6& subsection 1.2.3    \\ 
\hline
\end{tabular}
\end{center}

\end{document}
墙角的松树
墙角的松树 2022-09-16
这家伙很懒,什么也没写!

k

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览