You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LaTeX含下划线标签的自定义表格列表失效问题求助

带标签的自定义表格列表(含下划线标签)修复方案

问题描述

我尝试实现包含标签的自定义表格列表命令\listoftableswithrefs,但当表格标签中包含下划线时,该功能无法正常运行。已使用\detokenize命令处理标签但问题仍未解决,且无法修改含下划线的标签格式。

原代码

\documentclass[12pt, a4paper]{article}
\usepackage{dcolumn}
\usepackage{placeins}
\usepackage{bold-extra} % 消除字体警告
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{makecell}
\usetikzlibrary{calc}
\tikzset{%
    every node/.style={minimum width=55mm, minimum height=1cm, 
    draw=black, fill=white},
}
\usepackage{hyperref}


% 创建带标签的自定义表格列表命令
\makeatletter
\newcommand{\listoftableswithrefs}{%
  \section*{\listtablename}%
  \@mkboth{\MakeUppercase\listtablename}{\MakeUppercase\listtablename}%
  \@starttoc{lotref}%
}

% 存储表格标签信息
\newcommand{\@tablelabel}{}

% 重定义caption,同时写入.lot和.lotref文件
\let\oldcaption\caption
\renewcommand{\caption}[2][\relax]{%
  \gdef\@tablelabel{}% 重置标签存储
  \ifx#1\relax
    \oldcaption{#2}%
    \addcontentsline{lotref}{table}{\protect\numberline{\thetable}#2}%
  \else
    \oldcaption[#1]{#2}%
    \addcontentsline{lotref}{table}{\protect\numberline{\thetable}#1}%
  \fi
}

% 钩子到hyperref的label命令 - 此处需要修改
\AtBeginDocument{%
  \let\hyperref@label\label
  \renewcommand{\label}[1]{%
    \hyperref@label{#1}%
    \ifx\@captype\@undefined\else
      \ifx\@captype\@tabletype
        \addtocontents{lotref}{\protect\contentsline{table}{\hspace{2em}(Label: \texttt{\detokenize{#1}})}{\thepage}{#1}}%
      \fi
    \fi
  }%
  \def\@tabletype{table}%
}
\makeatother

\begin{document}

\listoftableswithrefs

\begin{table}[ht]
  \centering
  \begin{tabular}{cc}
    A & B \\ \hline
    C & D
  \end{tabular}
  \caption{Full caption}
  \label{tab:example}
\end{table}

% 测试含下划线的标签
\begin{table}[ht]
  \centering
  \begin{tabular}{cc}
    A & B \\ 
  \end{tabular}
  \caption{Full caption with underscore label}
  \label{tab:old_example}
\end{table}

\begin{figure}[hbt!]
\centering
\begin{tikzpicture}
        \node at (0,0) (start) {\textcolor{black}{\makecell[c]{\textbf{{Randomization}}}}};
        \node[anchor=north] at ($(start.south) + (-3,-.75) $) (lv0a) {\textcolor{black}{\makecell[c]{\textbf{\textsc{Treatment}}}}};
        \node[anchor=north] at ($(start.south) + (3,-.75)$) (lv0b) {\textcolor{black}{\makecell[c]{\textbf{\textsc{Control}}}}};
        \draw[black] (start.south) --++ (0,-0.5) -| (lv0a.north);
        \draw[black] (start.south) --++ (0,-0.5) -| (lv0b.north);
\end{tikzpicture}
\caption{Overview of experimental design}
\label{fig:experimentaldesign}
\end{figure}

\clearpage

\begin{table}[!htbp] 
  \centering 
  \caption{My caption} 
  \label{tab:results_two} 
\begin{tabular}{@{\extracolsep{5pt}}lD{.}{.}{-3} D{.}{.}{-3} } 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
\\[-1.8ex] & \multicolumn{1}{c}{All} & \multicolumn{1}{c}{All but always abstainers} \\ 
\hline \\[-1.8ex] 
 Treatment  & -0.028 & -0.013 \\ 
  & (0.059) & (0.075) \\ 
  Constant & -0.922^{***} & -1.545^{***} \\ 
  & (0.043) & (0.055) \\ 
 \hline \\[-1.8ex] 
Observations & \multicolumn{1}{c}{7,560} & \multicolumn{1}{c}{6,600} \\ 
Akaike Inf. Crit. & \multicolumn{1}{c}{8,957.504} & \multicolumn{1}{c}{6,110.444} \\ 
\hline 
\hline \\[-1.8ex] 
\textit{Note:}  & \multicolumn{2}{r}{$^{*}$p$<$0.1; $^{**}$p$<$0.05; $^{***}$p$<$0.01} \\ 
\end{tabular} 
\end{table} 

Table~\ref{tab:results_two} shows the results.  

\end{document}

修复方案

问题根源是:含下划线的标签写入辅助文件.lotref时,LaTeX会将下划线解析为数学模式命令,导致编译异常。需在写入辅助文件时正确保护\detokenize命令,确保下划线被当作普通字符处理。

修改\AtBeginDocument中的\label重定义部分,将标签内容用\protected@edef预先处理,再写入辅助文件:

\AtBeginDocument{%
  \let\hyperref@label\label
  \renewcommand{\label}[1]{%
    \hyperref@label{#1}%
    \ifx\@captype\@undefined\else
      \ifx\@captype\@tabletype
        % 预先处理标签,确保下划线被正确转义
        \protected@edef\@temp@label{\texttt{\detokenize{#1}}}%
        \addtocontents{lotref}{\protect\contentsline{table}{\hspace{2em}(Label: \@temp@label)}{\thepage}{#1}}%
      \fi
    \fi
  }%
  \def\@tabletype{table}%
}

原理说明

  • \protected@edef:定义一个受保护的扩展命令,确保\detokenize处理后的下划线不会被LaTeX提前解析。
  • 写入辅助文件时,\@temp@label中的内容已经是处理好的纯文本下划线,避免了编译时的命令解析错误。

测试验证

取消原代码中含下划线标签的注释(\label{tab:old_example}),编译后自定义表格列表会正确显示带下划线的标签,无编译错误。


内容的提问来源于stack exchange,提问作者canIchangethis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 20:34:53