2

I want to have the names of sections and subsections included in my List of Figures.

So for instance I want to see something like this in ToC:

1. Figures                             1

But I do not want to have 1. Figures when section with figures actually begins. I tried to use \section[Figures] instead of \section{Figures} but I still have the number of the section in the former case. Can I get rid of that?

EDIT: My final goal is to get something like this

1. Figures                          1
    1.1. Excitation energies        1
        1.1.1. Figure 1             1
        1.1.2. Figure 2             2

etc.

  • Are you trying to add an entry for your list of figures in the table of contents? – Johannes_B Jan 2 '16 at 11:20
  • Well, yes and no. Let me edit the question. – user2300369 Jan 2 '16 at 11:24
  • Do you want a subdivided list of figures? – Johannes_B Jan 2 '16 at 11:36
  • I'm not sure if I understand you, but I want the TOC to show page for each figure as well as gather them into sections and subsection. – user2300369 Jan 2 '16 at 11:45
  • Like this: Include chapters in List of Figures with titletoc? – Rico Jan 2 '16 at 11:47
2

Here's a partial expl3 rewrite of the code I posted at List of tables (or figures) by section - LaTeX

The code is shorter now, but at the moment a strange mixture of LaTeX and expl3 syntax. I will change this from time to time.

The most important features are the commands \EnableSectionsinLOFT and \AppendixSectionsInLOFT.

The document must be compiled 3 times at the moment, I try to reduce the compilation sequence to 2 runs, however.

By default, the appendix section names do not appear in the LoF and LoT.

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

\usepackage[demo]{graphicx}%

\usepackage{blindtext}
\usepackage{caption}
\usepackage{placeins}
\usepackage{xassoccnt}
\usepackage{morewrites}
\usepackage{xparse}

\makeatletter


\ExplSyntaxOn

\bool_new:N \g_sectionsinloft_bool
\bool_new:N \g_isappendix_bool
\bool_new:N \g_appendixinlof_bool
\bool_new:N \g_appendixinlot_bool

% Get some file handles first --> rewrite with expl3 later on

\newwrite\figurecountsout%
\newwrite\tablecountsout%

\newread\figurecountsin%
\newread\tablecountsin%

% The number of runs...
\DeclareDocumentCounter{localnumberofruns}%

\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}}%


% Some wrappers

\newcommand{\EnableSectionsInLOFT}{%
  \bool_gset_true:N \g_sectionsinloft_bool
}%

\newcommand{\DisableSectionsInLOFT}{%
  \bool_gset_false:N \g_sectionsinloft_bool
}%

\newcommand{\EnableAppendixSectionInLOF}{%
  \bool_gset_true:N \g_appendixinlof_bool
}%

\newcommand{\DisableAppendixSectionInLOF}{%
  \bool_gset_false:N \g_appendixinlof_bool
}%

\newcommand{\EnableAppendixSectionInLOT}{%
  \bool_gset_true:N \g_appendixinlot_bool
}%

\newcommand{\DisableAppendixSectionInLOT}{%
  \bool_gset_false:N \g_appendixinlot_bool
}%

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


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


\let\latex@@appendix\appendix

\renewcommand{\appendix}{%
  \bool_gset_true:N \g_isappendix_bool%
  \latex@@appendix%
  \DisableAppendixLOFT%
}


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

% localnumberofruns = 1 --> write figcount file --> no adding to lof
% localnumberofruns = 2 --> Read  fig counters file and decide if a section entry is to be done
% localnumberofruns = 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{\StoreCounterValue}[1]{%
  \immediate\expandafter\write\csname #1countsout\endcsname{%
    \number\value{#1}%
  }%
}%

\newcommand{\writesectionnametofile}[4]{%
  \int_compare:nNnT {\csname lastsection#4\endcsname} > {\c_zero }
  {   \bool_if:NTF \g_isappendix_bool {%
      \bool_if:cT { g_appendixin#1_bool } {%
        \addtocontents{#1}{\contentsline{#2}{#3}{}}%
      }%    
    }{%
      \addtocontents{#1}{\contentsline{#2}{#3}{}}%
    }%
  }%
}

\RenewDocumentCommand{\section}{som}{%
  \IfBooleanTF{#1}{%
    \LaTeXStandardSection*{#3}%
  }{% No starred section 
    \IfValueTF{#2}{%
      \edef\@@tempashortsectionname{#2}%
    }{%
      \edef\@@tempashortsectionname{#3}%      
    }%
    \bool_if:NT \g_sectionsinloft_bool {%
      \FloatBarrier%
      \gdef\lastsectionfigures{0}%
      \gdef\lastsectiontables{0}%
      \ifnum\value{localnumberofruns} = \secfig@@writecounterstage
      \gdef\lastsectionfigures{\number\value{figure}}%
      \gdef\lastsectionfigures{\number\value{table}}%
      \ifnum\value{totalsections} > \z@
      \StoreCounterValue{figure}%
      \StoreCounterValue{table}%
      \fi
      \else
      \LoadAllStoredCounterValues{localnumberofruns}%
      \fi
    }%
    % \phantomsection Use here if hyperref is needed
    \LaTeXStandardSection[\@@tempashortsectionname]{#3}%
    \bool_if:NT \g_sectionsinloft_bool {%
      \ifnum\value{localnumberofruns} = \secfig@@writecontentstage % Only make an entry in stages >= 2
      % Generalize for other tocs as well later on using a \seq_... variable
      \writesectionnametofile{lof}{section}{\@@tempashortsectionname}{figures}%  
      \writesectionnametofile{lot}{section}{\@@tempashortsectionname}{tables}%
      \fi
    }% End of \bool_if:NT 
  }%
}


\newcommand{\LoadAllStoredCounterValues}[1]{%
  \ifnum\value{#1} = \secfig@@readcounterstage
  \read\figurecountsin to \lastsectionfigures%
  \read\tablecountsin  to \lastsectiontables%
  \fi
}%

\AtBeginDocument{%
  \bool_if:NT \g_sectionsinloft_bool {%
    \typeout{Previous Stage  \number\value{localnumberofruns}}%
    \stepcounter{localnumberofruns}%
    \typeout{Current Stage is now \number\value{localnumberofruns}}%
    \ifnum\value{localnumberofruns} = \secfig@@writecounterstage
    \immediate\openout\figurecountsout=\jobname.figcount
    \immediate\openout\tablecountsout=\jobname.tabcount
    \else
    \openin\figurecountsin=\jobname.figcount%
    \openin\tablecountsin=\jobname.tabcount%
    \fi
  }%
}%



\AtEndDocument{%
  \bool_if:NT \g_sectionsinloft_bool {%
%  \ifsectionsinloft
    \ifnum\value{localnumberofruns} = \secfig@@resetstage
    \setcounter{localnumberofruns}{\secfig@@voidstage} % Reset counters afterwards. 
    \fi
    \immediate\write\@auxout{%
      \string\setcounter{localnumberofruns}{\number\value{localnumberofruns}}
    }
    % Write the last figure counter value to the file!
    \ifnum\value{localnumberofruns} = \secfig@@writecounterstage
    \StoreCounterValue{figure}%
    \StoreCounterValue{table}%
    \immediate\closeout\figurecountsout%
    \immediate\closeout\tablecountsout%
    \fi
    \immediate\closein\figurecountsin% Close input file 
    \immediate\closein\tablecountsin% Close input file 
  }%
}%

\ExplSyntaxOff


\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}

Next iteration

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

\usepackage[demo]{graphicx}%

\usepackage{blindtext}
\usepackage{caption}
\usepackage{placeins}
\usepackage{xassoccnt}
\usepackage{xparse}

\makeatletter


\ExplSyntaxOn

\bool_new:N \g_sectionsinloft_bool
\bool_new:N \g_isappendix_bool
\bool_new:N \g_appendixinlof_bool
\bool_new:N \g_appendixinlot_bool


\iow_new:N \l_figurecounts_out_stream
\iow_new:N \l_tablecounts_out_stream

\ior_new:N \l_figurecounts_in_stream
\ior_new:N \l_tablecounts_in_stream

\int_new:N \l_lastfigures_int
\int_new:N \l_lasttables_int


% The number of runs...
\DeclareDocumentCounter{localnumberofruns}%

\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}}%


% Some wrappers

\newcommand{\EnableSectionsInLOFT}{%
  \bool_gset_true:N \g_sectionsinloft_bool
}%

\newcommand{\DisableSectionsInLOFT}{%
  \bool_gset_false:N \g_sectionsinloft_bool
}%

\newcommand{\EnableAppendixSectionInLOF}{%
  \bool_gset_true:N \g_appendixinlof_bool
}%

\newcommand{\DisableAppendixSectionInLOF}{%
  \bool_gset_false:N \g_appendixinlof_bool
}%

\newcommand{\EnableAppendixSectionInLOT}{%
  \bool_gset_true:N \g_appendixinlot_bool
}%

\newcommand{\DisableAppendixSectionInLOT}{%
  \bool_gset_false:N \g_appendixinlot_bool
}%

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


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


\let\latex@@appendix\appendix

\renewcommand{\appendix}{%
  \bool_gset_true:N \g_isappendix_bool%
  \latex@@appendix%
  \DisableAppendixLOFT%
}


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

% localnumberofruns = 1 --> write figcount file --> no adding to lof
% localnumberofruns = 2 --> Read  fig counters file and decide if a section entry is to be done
% localnumberofruns = 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{\StoreCounterValue}[1]{%
  % Write expanded to stream
  \iow_now:cx {l_#1counts_out_stream} {\number\value{#1}}
}%


\newcommand{\writesectionnametofile}[4]{%
  \int_compare:nNnT {\int_use:c{l_last#4_int}} > {\c_zero }
  {   \bool_if:NTF \g_isappendix_bool {%
      \bool_if:cT { g_appendixin#1_bool } {%
        \addtocontents{#1}{\contentsline{#2}{#3}{}}%
      }%    
    }{%
      \addtocontents{#1}{\contentsline{#2}{#3}{}}%
    }%
  }%
}

\RenewDocumentCommand{\section}{som}{%
  \IfBooleanTF{#1}{%
    \LaTeXStandardSection*{#3}%
  }{% No starred section 
    \IfValueTF{#2}{%
      \edef\@@tempashortsectionname{#2}%
    }{%
      \edef\@@tempashortsectionname{#3}%      
    }%
    \bool_if:NT \g_sectionsinloft_bool {%
      \FloatBarrier%
      \int_zero:N \l_lastfigures_int
      \int_zero:N \l_lasttables_int
      \int_compare:nNnTF{\number\value{localnumberofruns}} = {\secfig@@writecounterstage} {% Only make an entry in stages >= 2
        \int_set:Nn \l_lastfigures_int {\number\value{figure}}
        \int_set:Nn \l_lasttables_int {\number\value{table}}
        \int_compare:nNnT {\number\value{totalsections} } > {\c_zero } {
          \StoreCounterValue{figure}%
          \StoreCounterValue{table}%
        }%
      }{%
        \LoadAllStoredCounterValues{localnumberofruns}%
      }%
    }%
    % \phantomsection Use here if hyperref is needed
    \LaTeXStandardSection[\@@tempashortsectionname]{#3}%
    \bool_if:NT \g_sectionsinloft_bool {%
      \int_compare:nNnT{\number\value{localnumberofruns}} = {\secfig@@writecontentstage} {% Only make an entry in stages >= 2
        % Generalize for other tocs as well later on using a \seq_... variable
        \writesectionnametofile{lof}{section}{\@@tempashortsectionname}{figures}%  
        \writesectionnametofile{lot}{section}{\@@tempashortsectionname}{tables}%
      }% End of \int_compare:nNnT
    }% End of \bool_if:NT 
  }%
}


\newcommand{\LoadAllStoredCounterValues}[1]{%
  \int_compare:nNnT {\value{#1}} = { \secfig@@readcounterstage }
  {
    \ior_get_str:NN \l_figurecounts_in_stream \l_tmpa_tl
    \ior_get_str:NN \l_tablecounts_in_stream \l_tmpb_tl
    \int_set:Nn \l_lastfigures_int {\tl_use:N \l_tmpa_tl}
    \int_set:Nn \l_lasttables_int {\tl_use:N \l_tmpb_tl}
  }
}%


\AtBeginDocument{%
  \bool_if:NT \g_sectionsinloft_bool {%
    \typeout{Previous~Stage~\number\value{localnumberofruns}}%
    \stepcounter{localnumberofruns}%
    \typeout{Current~Stage~is~now~\number\value{localnumberofruns}}%
    \int_compare:nNnT {\number\value{localnumberofruns}} = {\secfig@@writecounterstage} {%
      \iow_open:Nn \l_figurecounts_out_stream {\jobname.figcount}
      \iow_open:Nn \l_tablecounts_out_stream {\jobname.tabcount}
    }{%
      \ior_open:Nn \l_figurecounts_in_stream {\jobname.figcount}
      \ior_open:Nn \l_tablecounts_in_stream {\jobname.tabcount}
    }%
  }%
}%




\AtEndDocument{%
  \bool_if:NT \g_sectionsinloft_bool {%
    \int_compare:nNnT {\number\value{localnumberofruns}} = {\secfig@@resetstage} {%
      \setcounter{localnumberofruns}{\secfig@@voidstage} % Reset counters afterwards. 
    }   
    \immediate\write\@auxout{%
      \string\setcounter{localnumberofruns}{\number\value{localnumberofruns}} % Not expl3 yet
    }
    % Write the last figure counter value to the file!
    \int_compare:nNnT {\number\value{localnumberofruns}} = {\secfig@@writecounterstage} {%
      \StoreCounterValue{figure}%
      \StoreCounterValue{table}%
      \iow_close:N \l_figurecounts_out_stream
      \iow_close:N \l_tablecounts_out_stream
    }
    % Close the in - streams
    \ior_close:N \l_figurecounts_in_stream
    \ior_close:N \l_tablecounts_in_stream
  }%
}%

\ExplSyntaxOff


\makeatother    

% Perhaps use `chngcntr` rather...

\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}

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.