1

I am using stata to generate a table. This STATA generated tex table has label defined within \caption{} command. This labeling should work. But it gives me unresolved references (Table ??). When I manually take out the label which is inside the caption command and post it after \caption{}, the labeling works.

I am struggling to find the problem here. Is there any other way to give command in STATA that will keep \label{} after \caption{}. Or Can I keep using the same format with some modification?

{
  \begingroup
  \let\oldcaption\caption
  \renewcommand{\caption}[1]{\oldcaption{Regression table}}
  \renewcommand\normalsize{\small}%
  \normalsize
  \begin{center}
   \input{../RESULTS/table.tex}        
  \end{center}
 \endgroup
}
2
  • Welcome to TeX.SX! Please help us help you by posting a minimal compilable code example (MWE). Also, what is your question (which modifications would you want to do, what is your aim)?
    – TeXnician
    Oct 24, 2018 at 9:10
  • I want the label to work. No particular modifications just the labeling to work.
    – vikram
    Oct 24, 2018 at 9:40

1 Answer 1

1

If I understand the question correctly, you have some auto-generated LaTeX code which uses \caption{...\label{...}...} and you would like to change the caption text while preserving the label. This MWE would show the problem:

\documentclass{article}
\begin{document}

\let\oldcaption\caption
\renewcommand{\caption}[1]{\oldcaption{Regression table}}

% This table represents the auto-generated code
\begin{table}
    \centering
    \caption{A table\label{A}}
\end{table}

\ref{A} % does not work since the caption text and label was replaced
\end{document}

One possible solution would be using the helper macro \caption@getlabel offered by the caption package which tries to extract the \label from it's argument:

\documentclass{article}
\usepackage{caption3}
\begin{document}

\makeatletter
\let\oldcaption\caption
\renewcommand{\caption}[1]{%
  \oldcaption{Regression table}%
  \caption@getlabel#1\label{}\@nil
  \caption@thelabel
}
\makeatother

\begin{table}
    \centering
    \caption{A table\label{A}}
\end{table}

\ref{A}
\end{document}

Note that only caption3 is loaded (and not caption) since this package only provides helper macros and no re-definition of existing ones, so it should be compatible with all document classes. (If you load the caption package anyway you could drop \usepackage{caption3}.)

Caveat: \caption@getlabel is an internal macro, not documented in the user documentation of the caption package. But I (as author and maintainer of the caption package bundle) do not intend to change its interface in the future.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .