0

I want tables in the main body to be numbered 1,2,3, ... and in the Appendix A4, A5, A6, ....

References from the main body to tables should additionally make clear where that table is, by saying "Table 1", "Table 2", but "Appendix Table A4". This works great with cleveref. Yet, when a reference to the appendix is made from within the appendix this seems unnecessary. So in the appendix I want a reference to an appendix table to just be "Table A1".

The last bit eludes me. Here's what I have so far:

\documentclass{article}
\usepackage{cleveref}
\crefname{apptab}{Appendix Table}{Appendix Tables}

\begin{document}
    \section{maintext}
        Bla bla, reference to \Cref{tab:maintext} and to \Cref{tab:appendix}
        \begin{table}[h]\caption{This is a table in the main text}\label{tab:maintext}
            [X]
        \end{table}

    \crefname{apptab}{Table}{Tables}    %THIS DOES NOT OVERWRITE THE PREAMBLE STATEMENT
    \crefalias{table}{apptab}
    \renewcommand{\thetable}{A\arabic{table}}

    \section{appendix}
        Bla bla, reference to \Cref{tab:maintext} and to \Cref{tab:appendix}
        \begin{table}[h]\caption{This is a table in the appendix}\label{tab:appendix}
            [Y]
        \end{table}
    \end{document}

enter image description here

1

As you already found out once the name is set in the preamble it cannot be redefined mid document. This is because the label names are saved in the label's data at the point where the label appears.

Instead, what about creating a conditional so that the label name changes depending on where it is produced?

\documentclass{article}
\usepackage{cleveref}
\newif\ifinappendix\inappendixfalse
\crefname{apptab}
  {\protect{\ifinappendix\else Appendix \fi}Table}
  {\protect{\ifinappendix\else Appendix \fi}Table}

\begin{document}
  \section{maintext}
      Bla bla, reference to \Cref{tab:maintext} and to \Cref{tab:appendix}
      \begin{table}[h]\caption{This is a table in the main text}\label{tab:maintext}
          [X]
      \end{table}

\inappendixtrue

  \crefalias{table}{apptab}
  \renewcommand{\thetable}{A\arabic{table}}
  \section{appendix}
      Bla bla, reference to \Cref{tab:maintext} and to \Cref{tab:appendix}
      \begin{table}[h]\caption{This is a table in the appendix}\label{tab:appendix}
          [Y]
      \end{table}
\end{document}

The \protect there prevents the ifs from being expanded too early.

Of course this can be made cleaner by using \appendix and adding the setting of the inappendix flag in that macro, for example with \g@addto@macro\appendix{\inappendixtrue}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.