Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying to implement a style to name tables and figures depending on the section for a thesis and can't figure out how to do it with LaTeX.

In the end it should look like this: enter image description here

and my document:

\documentclass{article}

\begin{document}

\section{Start}
Start text
\section{Middle}
Middle text
\begin{table}[h]
    \centering
    \begin{tabular}{l}
    first table\\
    \end{tabular}
\caption{A normal table}
\end{table}

\section*{Supplemental}

\begin{table}[h]
    \centering
    \begin{tabular}{l}
        table in supplemental part\\
    \end{tabular}
\caption{This should read: ``Table S1: caption text''}
\end{table}

\end{document}   

I don't want to change the naming permanently, it should continue normally in the next chapter. Is there any easy way to accomplish something like this?

share|improve this question
    
Does this also require hyperref functionality? Why do you mention chapter when your MWE contains no \chapter and uses article? –  Werner Jan 16 '12 at 23:01
    
The functionality of hyperref is not necessary but would be nice of course. \chapter is in my original document but I wanted to keep the example small, just assume next section instead of chapter. –  Alexander Jan 16 '12 at 23:13
add comment

2 Answers

up vote 6 down vote accepted

The following solution does not support hyperref since it re-uses counters associated with supplemental tables (or figures).

enter image description here

\documentclass{article}
\usepackage{titlesec}% http://ctan.org/pkg/titlesec
\newcounter{mytable}\newcounter{myfigure}
\titleformat{\section}% <command>
  [hang]% <shape>
  {\setcounter{mytable}{\value{table}}\setcounter{myfigure}{\value{figure}}% Store table/figure counters
   \setcounter{table}{0}\global\def\thetable{S\arabic{table}}%
   \setcounter{figure}{0}\global\def\thefigure{S\arabic{figure}}%
   \normalfont\Large\bfseries}% <format>
  {\setcounter{table}{\value{mytable}}\setcounter{figure}{\value{myfigure}}% Restore table/figure counters
   \global\def\thetable{\arabic{table}}%
   \global\def\thefigure{\arabic{figure}}%
   \thesection}% <label>
  {2.3ex plus.2ex}% <sep>
  {}% <before code>
  %[]% <after code>
\begin{document}

\tableofcontents
\listoftables

\section{Start}
Start text

\section{Middle}
Middle text
\begin{table}[ht]
    \centering STUFF
\caption{A normal table}
\end{table}

\begin{table}[ht]
    \centering STUFF
\caption{A normal table}
\end{table}

\section*{Supplemental}

\begin{table}[ht]
    \centering SUPPLEMENTAL STUFF
\caption{A supplemental table}
\end{table}

\begin{table}[ht]
    \centering SUPPLEMENTAL STUFF
\caption{A supplemental table}
\end{table}

\section{Last}
End text
\begin{table}[ht]
    \centering STUFF
\caption{A normal table}
\end{table}

\begin{table}[ht]
    \centering STUFF
\caption{A normal table}
\end{table}

\section*{Supplemental}

\begin{table}[ht]
    \centering SUPPLEMENTAL STUFF
\caption{A supplemental table}
\end{table}

\begin{table}[ht]
    \centering SUPPLEMENTAL STUFF
\caption{A supplemental table}
\end{table}

\end{document}

I've used titlesec to hook into the sectional unit \section that conditionally executes certain parts of typesetting the heading (the settings are kept similar to the original article formatting). The label and separator part of the heading is only called when using \section, and therefore not "called" or used in \section*.

The concept is based on storing the current value of the table (and figure) counter in mytable (myfigure), after which it is set to 0. Then, only if you're using \section its value is restored. At both instances the display of the counter is renewed to prepend S or not.

share|improve this answer
    
Thank you! This does exactly what I intended and is highly appreciated with the coming deadline. –  Alexander Jan 17 '12 at 2:08
add comment

If I understand your objective correctly, you're aiming to number the tables (and figures?) as plain integers starting with 1 in the "main" body of the text, and to restart the numbering at 1, but now with an "S" prefixed, once you're in the supplemental part of the document.

You don't indicate if you've already defined a special command to make some modifications to the layout and other aspects of the document's appearance, so I'll assume you haven't done so. A relatively clean way to proceed would be to define a new command, say \startsupplement, in the preamble and to invoke this command at the start of that part of the document. This new command could be defined as follows:

\newcommand\startsupplement{%
    \makeatletter 
       \setcounter{table}{0}
       \renewcommand{\thetable}{S\arabic\c@table}
       \setcounter{figure}{0}
       \renewcommand{\thefigure}{S\@arabic\c@figure}
    \makeatother}
share|improve this answer
    
Thank you, this is almost what I thought of but I still need the counters after the supplement in the next part of the document. –  Alexander Jan 17 '12 at 2:11
    
Let me make sure I understand: The supplemental section doesn't occur at the end of the document but might occur anywhere (say, at the end of a chapter)? After the supplement, then, you want to go back to "regular" numbering (without the "S" prefix) of tables and figures? –  Mico Jan 17 '12 at 2:14
    
Mico: exactly, each chapter gets a supplemental section and then then next chapter starts and should use the "regular" numbering again. The background is the following, the content of the chapters comes from separate scientific papers, each paper has it's own supplemental and the whole document should reflect this structure. –  Alexander Jan 17 '12 at 12:07
    
It would be helpful to know which LaTeX document class you use. (For instance, if you were using the book document class, or one that's derived from book, all figure and table numbers would be reset at the start of each chapter automatically. Therefore, there'd be no need to pursue anything more elaborate than what's implemented in the \startsupplement command above. For now, I must surmise you're not using the book document class.) Armed with knowledge of the type of document class you do employ, it may be possible to design a setup that is compatible with hyperref. –  Mico Jan 17 '12 at 13:52
    
Thanks for all the thought you put into this. I have not specified a class because I am still trying out different options. So far I scrbook seems to work nicely. –  Alexander Jan 17 '12 at 14:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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