开发者

Using a single count for figures and tables in LaTeX

开发者 https://www.devze.com 2023-01-18 16:23 出处:网络
I\'ve been given a LaTeX document to edit with the following code for using a single count for figures and tables.

I've been given a LaTeX document to edit with the following code for using a single count for figures and tables.

 \makeatletter
 \newcounter{unisequence}
 \def\ucaption{
    \ifx\@captype\@undefined
        \@latex@error{\noexpand\ucaption outside float}\@ehd
        \expandafter\@gobble
    \else
        \refstepcounter{unisequence} 
        \expandafter\@firstofone
    \fi
    {\@dblarg{\@caption\@capt开发者_StackOverflow社区ype}}
  }
 \def\thetable{\@arabic\c@unisequence}
 \def\thefigure{\@arabic\c@unisequence}
 \makeatother

This works well to give a single counter for captions of tables and figures but, I find that if I click on any of the caption numbers in the .pdf this code generates, I am always returned to the first figure or table in the document rather than the one I want, e.g. clicking on in Table [3] will take me to Table 1 instead.

Does anyone know how to fix this? Or can anyone advise an alternative?

I am a LaTeX newbie.

Thanks

Mr Morgan.


Based on https://stackoverflow.com/a/3866061/53974 and https://tex.stackexchange.com/a/127744/1340, we can just (1) make the table counter be the same as the figure counter, and (2) make the table float type be the same as the figure float type, to ensure that the ordering is consistent with the numbering, because:

LaTeX keeps all floats of the same type in order

Code:

\makeatletter
\let\c@table\c@figure % for (1)
\let\ftype@table\ftype@figure % for (2)
\makeatother

Compared to https://stackoverflow.com/a/3866061/53974, this keeps \thetable and \thefigure alone—so the table and figure numbers are kept formatted as-is. This respects per-chapter/section numbering, and it works for me well together with hyperlinking, the subcaption, float and rotating packages, and probably more, on a 160-page document.


If it's of use to anyone, use:

\makeatletter
\renewcommand*{\thetable}{\arabic{table}}
\renewcommand*{\thefigure}{\arabic{figure}}
\let\c@table\c@figure
\makeatother 

In the preamble of your document.


Off the top of my head, this is a problem of the snippet you have there not generating the correct reference. See here, for instance: http://en.wikibooks.org/wiki/LaTeX/Hyperlinks


Combining the code in the query with a previous answer worked wonderfully for me - thank you.

To get the figures and tables numbered consecutively within chapters with chapter numbers too, I replaced two lines in the answer snippet by

\renewcommand*{\thetable}{\arabic{chapter}.\arabic{table}}
\renewcommand*{\thefigure}{\arabic{chapter}.\arabic{figure}}
0

精彩评论

暂无评论...
验证码 换一张
取 消