12

I am writing a master's thesis and I wish to create a list of tables and list of figures that is ordered by section, with the name of the section clearly displayed. The numbering of my tables (and figures) is already made per section, which is good.

I have seen some topic dealing with this already, but it all seems awefully complicated and improbable, with redefining of functions, and most importantly, it only delt with books (and chapters, which i don't have...). I thought it would be fairly simple and i have found myself browsing the web for quite some time now.

Would you have any easy solution? Attached is how i want my list of tables to look like.

Thanks a million Guillaume

That's basically how I want it to look like

cmhughes
  • 100,947
Guillaume
  • 145
  • 1
  • 5
  • Welcome to TeX.SX! 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}. –  Sep 09 '14 at 19:04
  • 1
    What \documentclass are you using? Also are you using any ToC-or section-related packges? – Werner Sep 09 '14 at 19:04
  • You are talking of section, but the image shows chapter entries –  Sep 09 '14 at 19:04
  • 2
    What should happen to chapters (or sections) that do not have a specific float (say, no figure in section 4; what's to happen to the heading of section 4 in the LoF)? – Werner Sep 09 '14 at 19:06
  • @Werner: A crucial aspect, yes. At first glance, it seems to have a query about the number of figures in the current section, writing that the .aux, etc. examining then in the 2nd run, writing the .lof entry and all will be 'correct' in the 3rd. run... At the moment, I only see a redefinition of \section ahead... –  Sep 09 '14 at 19:13
  • There is another problem: Figures/tables from one section may float to some place before the section they belong too –  Sep 09 '14 at 19:45
  • @Guillaume: I have added an extended version which addresses your further request –  Sep 12 '14 at 05:48

1 Answers1

13

Update -- Improved Version -- 09/12/2014

Update -- a similar question can be found here: Adding names of sections in List of Figures I provided a solution there as well, partially based on expl3 features.

This is a possible solution. There might be other (easier?) ones.

Features

  • EnableSectionsInLOFT enables globally the section separators, \DisableSectionsInLOFT disables them globally. At documentstart, it's enabled by default.
  • Appendix sections will not be listed as separators in LOT/LOF as default
  • Use \captionsetup{list=no} after \appendix to prevent the listing of tables/figures completely in LOF/LOT
  • \EnableAppendixSectionInLOF and \EnableAppendixSectionInLOT enable the generation of section separators from appendix sections. There are corresponding \Disable.... macros for disabling this feature

  • EnableAppendixLOFT enables the Appendix section separators globally, \DisableAppendixLOFT disables this.

  • The spacing of separators above and below can be changed by providing other values to the lengths \loftsectionbeforeskipamount and \loftsectionafterskipamount. Currently both default to \medskip.

I will explain, what I did.

  1. Each time, a \section command is used, it should write a line to the list of figures, but only, if there are some figures at all.

    In order to achieve this, some work has to be done for the \section command -- redefining it to the needs -- otherwise, it behaves as expected. In order to keep \tableofcontents from complaining \section* has to be 'redefined' too.

    It would be nice, all this could be with \xpatchcmd etc. but this is prevented by the interior definition of \section :-( --> go the long and hard way ;-)

  2. Figures are restricted to the current section and can not float into the text 'space' of previous/next sections -- this is done with \Floatbarrier

  3. The figures counter is stored to a file name \jobname.figcount and read in later on in the next stage (see definition of stages below)
  4. I decided to use \addtocontents instead of `\addcontentsline, to make a clearer distinction between the section before and the figure list.

All remarks from above hold for the table counter as well.

Definition of stages

  • Stage One --> the counter values are written to an external file

  • Stage Two --> The counter values are read in, section entries added to the LOF/LOT

  • Stage Three --> The necessary updated stage for the lof/loc

There are 3 stages for the compilation -- LaTeX must run three times!

Do not delete \jobname.tabcount and \jobname.figcount in between!


\documentclass[paper=a4,12pt]{scrartcl}%

\usepackage[demo]{graphicx}%

\usepackage{etoolbox}%
\usepackage{blindtext}%
\usepackage{caption}%
\usepackage{placeins}%
\usepackage{assoccnt}%
\usepackage{morewrites}%


\makeatletter

% Get some file handles first

\newwrite\figurecountsout%
\newwrite\tablecountsout%

\newread\figurecountsin%
\newread\tablecountsin%

% The number of runs...
\newcounter{numberofruns}%

\newcommand{\secfig@@voidstage}{0}%
\newcommand{\secfig@@writecounterstage}{1}%
\newcommand{\secfig@@readcounterstage}{2}%
\newcommand{\secfig@@writecontentstage}{2}%
\newcommand{\secfig@@resetstage}{3}%

\newlength{\loftsectionbeforeskipamount}%
\newlength{\loftsectionafterskipamount}%


\setlength{\loftsectionafterskipamount}{\medskipamount}%
\setlength{\loftsectionbeforeskipamount}{\medskipamount}%

\newcommand{\loftsectionbeforeskip}{\vspace{\loftsectionbeforeskipamount}}%
\newcommand{\loftsectionafterskip}{\vspace{\loftsectionafterskipamount}}%


\newtoggle{SectionsInLOFT}%

\newcommand{\EnableSectionsInLOFT}{%
  \settoggle{SectionsInLOFT}{true}%
}%

\newcommand{\DisableSectionsInLOFT}{%
  \settoggle{SectionsInLOFT}{false}%
}%


\EnableSectionsInLOFT%


\newtoggle{AppendixSectionsInLOF}%
\newtoggle{AppendixSectionsInLOT}%

\settoggle{AppendixSectionsInLOF}{true}%
\settoggle{AppendixSectionsInLOT}{true}%

\newcommand{\EnableAppendixSectionInLOF}{%
  \settoggle{AppendixSectionsInLOF}{true}%
}%

\newcommand{\EnableAppendixSectionInLOT}{%
  \settoggle{AppendixSectionsInLOT}{true}%
}%


\newcommand{\EnableAppendixLOFT}{%
\EnableAppendixSectionInLOF%
\EnableAppendixSectionInLOT%
}%

\newcommand{\DisableAppendixSectionInLOT}{%
  \settoggle{AppendixSectionsInLOT}{false}%
}%

\newcommand{\DisableAppendixSectionInLOF}{%
  \settoggle{AppendixSectionsInLOF}{false}%
}%

\newcommand{\DisableAppendixLOFT}{%
  \DisableAppendixSectionInLOF%
  \DisableAppendixSectionInLOT%
}%



\apptocmd{\appendix}{\DisableAppendixLOFT}%


% Needed for correct number of sections
\newcounter{totalsections}
\DeclareAssociatedCounters{section}{totalsections}%

% numberofruns = 1 --> write figcount file --> no adding to lof
% numberofruns = 2 --> Read  fig counters file and decide if a section entry is to be done
% numberofruns = 3 --> Last update run to the toc, do neither read nor write! Set the counter of runs to 0 then. 

\@addtoreset{figure}{section}%
\@addtoreset{table}{section}%

\let\LaTeXStandardSection\section%
\newcommand{\unstarredsection@@noopt}[1]{%
  % Call the command without option - long title will become 'short' title
  \unstarredsection@@opt[#1]{#1}%
}%

\newcommand{\StoreCounterValue}[1]{%
  \immediate\expandafter\write\csname #1countsout\endcsname{%
    \number\value{#1}%
  }%
}%


\newcommand{\LoadAllStoredCounterValues}[1]{%
  \ifnumequal{\number\value{#1}}{\secfig@@readcounterstage}{% Only read, if value is \secfig@@readcounterstage%
    \read\figurecountsin to \lastsectionfigures
    \read\tablecountsin  to \lastsectiontables%
  }{}%
}%

\newcommand{\loftsectioncontentsline}[1]{%
  \loftsectionbeforeskip\textbf{\large\thesection~#1}\loftsectionafterskip
}%

\newcommand{\unstarredsection@@opt}[2][]{%
  \iftoggle{SectionsInLOFT}{%
    \FloatBarrier%
    \gdef\lastsectionfigures{0}%
    \gdef\lastsectiontables{0}%
    \ifnumequal{\number\value{numberofruns}}{\secfig@@writecounterstage}{% Only write, if counter has value 1
      \gdef\lastsectionfigures{\number\value{figure}}%
      \gdef\lastsectionfigures{\number\value{table}}%
      \ifnumequal{\number\value{totalsections}}{0}{%
      }{%
        \StoreCounterValue{figure}%
        \StoreCounterValue{table}%
      }%
    }{%
      \LoadAllStoredCounterValues{numberofruns}%
    }%
  }{%
  }%
    % \phantomsection Use here if hyperref is needed
  \LaTeXStandardSection[#1]{#2}%
  \iftoggle{SectionsInLOFT}{%
    \ifnumequal{\number\value{numberofruns}}{\secfig@@writecontentstage}{% Only make an entry in stages >= 2
      \ifnumgreater{\lastsectionfigures}{0}{%
        \typeout{Writing a section entry for \thesection{}to the LOF}%
        \iftoggle{AppendixSectionsInLOF}{%
          \addtocontents{lof}{\loftsectioncontentsline{#1}}%
        }{}%
      }{%
        \typeout{No figures in \thesection!}
      }%
      \ifnumgreater{\lastsectiontables}{0}{%
        \typeout{Writing a section entry for \thesection{}to the LOT}%
        \iftoggle{AppendixSectionsInLOT}{%
          \addtocontents{lot}{\loftsectioncontentsline{#1}}%
        }{}%
      }{%
        \typeout{No tables in \thesection!}
      }%
    }{}%
  }{%
  }% End of \iftoggle{SectionsInLOFT}%
}%

\newcommand{\unstarredsection}{%
  \@ifnextchar[{%
    \unstarredsection@@opt%
  }{%
    \unstarredsection@@noopt%
  }%
}%

\newcommand{\starredsection}[1]{%
  \LaTeXStandardSection*{#1}%
}%

\renewcommand{\section}{%
  \@ifstar{%
    \starredsection%
  }{%
    \unstarredsection%
  }%
}%



\AtBeginDocument{%
  \iftoggle{SectionsInLOFT}{%
    \typeout{Previous Stage  \number\value{numberofruns}}%
    \stepcounter{numberofruns}%
    \typeout{Current Stage is now \number\value{numberofruns}}%
    \ifnumequal{\number\value{numberofruns}}{\secfig@@writecounterstage}{%
      \immediate\openout\figurecountsout=\jobname.figcount
      \immediate\openout\tablecountsout=\jobname.tabcount
    }{%
      \openin\figurecountsin=\jobname.figcount%
      \openin\tablecountsin=\jobname.tabcount%
    }%
  }{%
    % Nothing to be done!
  }%
}%

\AtEndDocument{%
  \iftoggle{SectionsInLOFT}{%
    \ifnumequal{\number\value{numberofruns}}{\secfig@@resetstage}{%
      \setcounter{numberofruns}{\secfig@@voidstage} % Reset counters afterwards. 
    }{}%
    \immediate\write\@auxout{%
      \string\setcounter{numberofruns}{\number\value{numberofruns}}
    }
    % Write the last figure counter value to the file!
    \ifnumequal{\number\value{numberofruns}}{\secfig@@writecounterstage}{%
      \StoreCounterValue{figure}%
      \StoreCounterValue{table}%
      \immediate\closeout\figurecountsout%
      \immediate\closeout\tablecountsout%
    }%
    \immediate\closein\figurecountsin% Close input file 
    \immediate\closein\tablecountsin% Close input file 
  }{%
    % no Sections in lof/lot
  }%
}%



\makeatother    


\renewcommand{\thefigure}{\arabic{section}.\arabic{figure}}%
\renewcommand{\thetable}{\arabic{section}.\arabic{table}}%

\EnableSectionsInLOFT%

\begin{document}
\tableofcontents%
\listoffigures 
\listoftables
\clearpage

\section{First}
\blindtext

\begin{figure}[!ht]
\begin{center}
  \includegraphics[scale=1]{somefig}%
  \caption{First}
\end{center}
\end{figure}

\begin{figure}
\begin{center}
  \includegraphics[scale=1]{somefig}%
  \caption{Second}
\end{center}
\end{figure}

\begin{table}
\begin{tabular}{ccc}
a & small table  \tabularnewline
\end{tabular}
\caption[Small Table]{First small table}
\end{table}

\section{Second}

\begin{figure}
  \caption{Third}
\end{figure}

\begin{figure}
  \caption{Fourth}
\end{figure}

\begin{figure}
  \caption{Fifth}
\end{figure}

\begin{figure}
  \caption{Sixth}%
\end{figure}


\section{Without figures}

\section[One with another title]{A very long, long, long section title}


\begin{figure}
  \caption{Seventh}
\end{figure}

\begin{figure}
  \caption[Eight]{Eight Section}%
\end{figure}


\section{Another section without figures}%

\blindtext

\section[Yippie]{Last one}%

\begin{table}
  \caption{dummy table}%
\end{table}

\appendix

\captionsetup{list=no}

\section{First Appendix Section}

\begin{figure}
  \caption{Appendix Figure}
\end{figure}

\begin{table}
  \caption[]{dummy table in appendix}
\end{table}

\section{Second Appendix Section}

\begin{figure}
  \caption{Appendix Figure Nr. 2}
\end{figure}

\begin{table}
  \caption{Dummy table in appendix }
\end{table}


\end{document}

enter image description here

Please give feedback, if this is worth to make package out of it.