简体   繁体   中英

Label not working inside a \newcommand with a custom Float in LaTeX

I declared a new environment containing a \caption and a \label so I can make references to it.

In my header:

\DeclareFloatingEnvironment[name=Tableau]{tableau}
\newenvironment{ptab}{\captionsetup{type=tableau}}{}

In my.tex document:

\begin{ptab}
    \caption{A caption for my table}
    \label{ptab:myTab}
\end{ptab}

Some text with a reference (Tableau~\ref{ptab:myTab}) % Works fine !

The problem: I want to gain some time by declaring a \newcommand that could write this for me. But the references in text are not working anymore !

Added in my header:

\newcommand{\tabref}[2]{%
    \begin{ptab} 
        \label{#1} 
        \caption{#2} 
    \end{ptab}}

In the.tex document:

\tabref{ptab:myTab}{A caption for my table}

Some text with a reference (Tableau~\ref{ptab:myTab}) % Not working "(Tableau ??)"

I an aware a similar question has already been asked before but it was not concerning new environment. How to reference a label within a newcommand in LATEX?

I found out that the order of \label{} and \caption{} was inverted. It is important since LaTeX needs to create a caption before it can be referenced with a label. Working code:

\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 !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM