[英]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.