3

Can I renew the table counter to use my own counter similarly to renewing, for instance, the \tablename? What is the keyword or where could I look for it?

This will be used within a newenvironment based on tabu (in order to have a separate counter from the table environment).

This is for the environment created in this question. When we click in a hyperlink in the PDF, we are always sent to the first Interface or last Table in the document. I believe the reason may be that hyperref relies on the table counter to work properly; if we comment out the counter decrease, \addtocounter{table}{-1}, in the environment definition then: hyperlinks work well, but table numbering is incorrect.

\documentclass{article}

\usepackage{array, booktabs, longtable, tabu}
\usepackage{hyperref}

\newcommand{\ListOfInterfacesName}{List of Interfaces}
\newcommand{\InterfaceName}{Interface}

% Define list of interfaces command
\makeatletter%
  \newcommand{\listofinterfaces}{%
    \section*{\ListOfInterfacesName}%
    \@starttoc{int}  % New toc with extension *.int%
    \clearpage%
  }%
\makeatother%

% Usage: {}{<label>}{<ifNum>}{<caption>}
\newenvironment{ryetable}[4]{%
  \renewcommand{\tablename}{\InterfaceName} % Safe to renew table because it is inside a group
  \renewcommand{\thetable}{#3} % Explicitly set table number to given argument (safe because redifinition also occurs inside a group)
  \addtocounter{table}{-1} % Reduce counter number since using table increases it automatically
  \addcontentsline{int}{subsection}{\protect{\numberline{#3}{#4}}} % Define entry subsection akin to normal tables
  \gdef\tmpheaders{#1} % Not currently used
  \gdef\tmplabel{\label{#2}} % Save label for later use
  \gdef\tmpifnum{#3} % Save interface number
  \gdef\tmpcaption{\caption[]{#4}} % Save caption for later use ---> drop the entry to the table list
  \begin{longtabu} to \textwidth {@{} X[1,l] X[1,l] X[1,l] >{\raggedright}X[1,l] @{}}
    \\\toprule\rowfont\bfseries
    Header1 & Header2 & Header3 & Description \\
    \midrule
  \endfirsthead
    \multicolumn{4}{@{}c@{}}{\small\textit{(continued)}}\\
    \toprule\rowfont\bfseries
    Header1 & Header2 & Header3 & Description \\
    \midrule
  \endhead
    \bottomrule
    \multicolumn{4}{@{}r@{}}{\small\textit{(continues)}}\\
  \endfoot
    \bottomrule
    \tmpcaption
    \tmplabel
  \endlastfoot
}{%
  \end{longtabu}
}

\begin{document}
\listoftables
\listofinterfaces %TODO
\section{MySection}
For a nifty new environment check Interface~\ref{if:myinterface}.
\begin{ryetable}{}{if:myinterface}{31}{This is my pretty interface.}
a & b & c & d\\
e & f & g & h\\
i & j & k & l\\
m & n & o & p\\
q & r & s & t\\
u & v & w & x\\
y & z & -- & --\\
\end{ryetable}

% Some other table to show on list
\begin{table}[h]
  \centering
  \begin{tabular}{c|c}
    a&b\\
    c&d\\
  \end{tabular}
  \caption{Caption of tabular table.}
  \label{tab:somelabel}
\end{table}

\newpage
\begin{ryetable}{}{if:myif2}{35}{This is the other interface.}
a & b & c & d\\
e & f & g & h\\
i & j & k & l\\
m & n & o & p\\
q & r & s & t\\
u & v & w & x\\
y & z & -- & --\\
\end{ryetable}
Go to Interface~\ref{if:myif2}. % Incorrectly links to first environment
\end{document}
Daniel
  • 429
  • 2
    Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  Jun 16 '14 at 16:41

1 Answers1

2

(long)tabu just inherits its counter usage from longtable (I'd just use the latter personally) but the issue is the same.

I've started to implement that as an option in a possible future version but in V4 it isn't directly available.

For simple use you can just locally reset the {table} counter to be the value of your custom counter and set \thetable the main issue is that longtable hardwires the list of tables in its caption handling so you will need to locally redefine

\makeatletter
\def\LT@c@ption#1[#2]#3{%
  \LT@makecaption#1\fnum@table{#3}%
  \def\@tempa{#2}%
  \ifx\@tempa\@empty\else
     {\let\\\space
     \addcontentsline{lot}{table}{\protect\numberline{\thetable}{#2}}}%
  \fi}
\makeatother

so it doesn't use \fnum@table (it could use My zzz \thezzz instead if you are using zzz counter) and similarly change lot and table to use whatever list of zzz file you have set up.)

David Carlisle
  • 757,742
  • Longtable skips/increases counter when no caption is used in the previous longtable... I could not renew the counter using \setcounter. Anyway out? – Khaaba Mar 26 '16 at 08:27
  • 1
    @Khaaba I'm not sure I understand the issue, you can always go \addtocounter{table}{-1} to undo longtable's increment if it is a problem. – David Carlisle Mar 26 '16 at 10:37
  • If possible, longtable counter may not increment if \caption is not in use in the longtable environment. At the moment, the counter increments even if \caption is not used. – Khaaba Mar 26 '16 at 13:02