[英]Label not working inside a \newcommand with a custom Float in LaTeX
我声明了一个包含\caption
和\label
的新环境,以便我可以引用它。
在我的 header 中:
\DeclareFloatingEnvironment[name=Tableau]{tableau}
\newenvironment{ptab}{\captionsetup{type=tableau}}{}
在 my.tex 文件中:
\begin{ptab}
\caption{A caption for my table}
\label{ptab:myTab}
\end{ptab}
Some text with a reference (Tableau~\ref{ptab:myTab}) % Works fine !
问题:我想通过声明一个可以为我写这个的\newcommand
来获得一些时间。 但是文本中的引用不再起作用了!
在我的 header 中添加:
\newcommand{\tabref}[2]{%
\begin{ptab}
\label{#1}
\caption{#2}
\end{ptab}}
在 .tex 文件中:
\tabref{ptab:myTab}{A caption for my table}
Some text with a reference (Tableau~\ref{ptab:myTab}) % Not working "(Tableau ??)"
我知道之前已经问过类似的问题,但它与新环境无关。 如何在 LATEX 的新命令中引用 label?
我发现\label{}
和\caption{}
的顺序颠倒了。 这很重要,因为 LaTeX 需要先创建标题,然后才能使用 label 引用它。 工作代码:
\newcommand{\tabref}[2]{%
\begin{ptab}
\caption{#2}
\label{#1}
\end{ptab}}
\tabref{ptab:myTab}{A caption for my table}
Some text with a reference (Tableau~\ref{ptab:myTab}) % Now working !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.