-1

I have the following situation: In my document the caption of a figure appears normal like "Figure 1: example". BUT in the List of Figures the caption appears like "1 example". Is there a way to have "Figure 1 example" in List of Figures too?

I am using XeLaTeX as a compiler and the code is the following:

\documentclass[twoside, a4paper, 11pt]{article}
\usepackage{caption}
\captionsetup[figure]{name=Figure}
\usepackage{subcaption}
\usepackage{float}
\usepackage{chngcntr}
\counterwithin{figure}{section}
\usepackage{graphicx}
\graphicspath{ {./Images/} }

\begin{document}

\tableofcontents

\renewcommand{\listfigurename}{List of Figures} \addcontentsline{toc}{subsection}{List of Figures} %Πρόσθεση στα περιεχόμενα (table of contents) \listoffigures

\begin{figure}[htb] \centering \includegraphics[width=1\textwidth]{example.png} \caption[Example for List of Figures]{Different example for caption under the figure.} \label{Fig:exmpl} \end{figure}

\end{document}

I found out that I can use this code:

\usepackage[titles]{tocloft}
\newlength{\mylen}

\renewcommand{\cftfigpresnum}{\figurename\enspace} \renewcommand{\cftfigaftersnum}{:} \settowidth{\mylen}{\cftfigpresnum\cftfigaftersnum} \addtolength{\cftfignumwidth}{\mylen}

BUT this messed up seriously the structrure of my document. Do I have to change something to my code or only add the above? I did not understand clearly. It really messed up my document...It messes my document to this: enter image description here

From this: enter image description here

DGuys
  • 57
  • 4
  • Please explain how tocloft is missing up the structure of your document. Without detailt we have no idea what exactly you are doing. – daleif Dec 06 '23 at 11:54
  • 1
    Please don't delete and repost your questions. The duplicate linked to your previous version had more than just the tocloft answer. Also if you don't tell us why this "messes up" your document, why do you assume that any answer you might get here, won't mess it up as well? – samcarter_is_at_topanswers.xyz Dec 06 '23 at 11:59
  • 2
    Note: Your \addcontentsline command is too early. If a page break happens just before the list of figures, the \addcontentsline command will still show the page number of the last page of the table of contents. Better use a package, that supports adding the entry to the table of contents. – cabohah Dec 06 '23 at 13:00
  • I just edited with screenshots in order for you to see the changes. – DGuys Dec 06 '23 at 13:03
  • 3
    Instead of showing a screenshot, you should show a minimal working example that can be used to reproduce that changes aka the problem. Otherwise the question would be unclear. – cabohah Dec 06 '23 at 13:22

2 Answers2

1

You can use package tocbasic for the list of figures:

\documentclass[twoside, a4paper, 11pt]{article}
\usepackage{caption}
\captionsetup[figure]{name=Figure}% IMHO not needed.
\usepackage{subcaption}% Not used in the example.
\usepackage{float}% Not used in the example.
\usepackage{chngcntr}% Note: Not needed with LaTeX versions of the last 5 years!
\counterwithin{figure}{section}
\usepackage{graphicx}
\graphicspath{ {./Images/} }% Not used in the example.

\usepackage{tocbasic} \setuptoc{lof}{totoc}% add an ToC entry for the LoF \renewcommand{\listoffigures}{\listoftoc[List of Figures]{lof}}% redefine \listoffigures to use tocbasic \DeclareTOCStyleEntry[% reconfigure the figure entries dynnumwidth,% automatic adapt the width reserved for the number (needs at % least 3 LaTeX runs!) % indent=0pt, % optional to remove the indent of the entries entrynumberformat=\figurenumberformat,% to change the number formatting ]{tocline}{figure} \newcommand{\figurenumberformat}[1]{Figure\enspace\ignorespaces #1\unskip:\hfil}

\begin{document}

\tableofcontents

\listoffigures

\begin{figure}[htb] \centering \includegraphics[width=1\textwidth]{example-image} \caption[Example for List of Figures]{Different example for caption under the figure.} \label{Fig:exmpl} \end{figure}

\end{document}

After at least three LaTeX runs you get:

after three LaTeX runs

See section “Configuring Content-List Entries” in chapter “Managing Content Lists with tocbasic” of the English KOMA-Script manual for more information.

Note: Using the tocbasic command \setuptoc{lof}{totoc} to add an entry for the List of Figures (lof) to the Table of Contents also solves the problem, that in your original example a page break can happen between adding that entry using \addcontentsline and printing the List of Figures using \listoffigures, which would result in a wrong number for this entry in the Table of Contents.

cabohah
  • 11,455
  • Do I need to delete these 2 lines (after the \listoffigures command I have) or not? \renewcommand{\listfigurename}{List of Figures} \addcontentsline{toc}{subsection}{List of Figures} – DGuys Dec 06 '23 at 13:05
  • @DGuys Testing my example should answer your question. Also reading the comments in my example. However, the first of the two lines is not needed any longer, but does no make any harm. So you can delete it or keep it. The second line was always wrong, because the List of Figures is a section not a subsection (but you can use \setuptoc[lof]{leveldown} to change this) and it is not correctly placed. See my comment to your question. – cabohah Dec 06 '23 at 13:09
  • I understand what you are saying but it still does mess up my document with the same way. I edited my answer with screenshots and I have the same changes with your solution. – DGuys Dec 06 '23 at 13:15
  • @DGuys Have you tested my example? Have you done at least three LaTeX runs? Do you get the same result with my example? If so, there is something in your real document, that you've not shown. Sorry, but nobody can help, if you don't make your problem in using tocbasic (or tocloft) reproducible. – cabohah Dec 06 '23 at 13:17
0

The least intrusive method is to locally redefine \numberline for the list of figures. \numberlineis used to print the number.

\documentclass[twoside, a4paper, 11pt]{article}
\usepackage{caption}
\captionsetup[figure]{name=Figure}
\usepackage{subcaption}
\usepackage{float}
\usepackage{chngcntr}
\counterwithin{figure}{section}
\usepackage{graphicx}
%\graphicspath{ {./Images/} }

\makeatletter \def\fignumline#1{\figurename~\hb@xt@@tempdima{#1\hfil}}% see \numberline \makeatother

\begin{document}

\tableofcontents

\bgroup% local redefinitions \renewcommand{\listfigurename}{List of Figures% \addcontentsline{toc}{subsection}{List of Figures}}% \let\numberline=\fignumline \listoffigures \egroup

\begin{figure}[htb] \centering \includegraphics[width=1\textwidth]{example-image} \caption[Example for List of Figures]{Different example for caption under the figure.} \label{Fig:exmpl} \end{figure}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
  • @DGuys Note: John has not fixed your wrongly places \addcontentsline. With this code still a page break can happen between the \addcontensline and the \listoffigure and therefore the wrong page number can be shown in the Table of Contents. – cabohah Dec 07 '23 at 08:42
  • Now, you are adding the list of figures already before the table of contents. This does not work if the table of contents is longer than one page. So IMHO the new version is even worse than the old one. As already told in a comment to the question: Best is, to use a package like tocbibind or tocbasic. ;-) – cabohah Dec 07 '23 at 15:23
  • @cabohah - Since article class does not \clearpage, this will work except at the very bottom of a page. You could insert \addcontentsline inside \listfigurename, but I was shooting for "least intrusive". – John Kormylo Dec 07 '23 at 15:26
  • Sorry, but you are wrong. The \addcontentsline{toc}{subsection}{\listfigurename} before \tableofcontents does not add the first page of the \listoffigures, but a it's best the first page of the \tableofcontents (and sometimes even the page before). So it is surely the wrong place. Even immediately before \listoffigures it can fail if the section header of the \listoffigures results in a new page. – cabohah Dec 07 '23 at 15:32
  • Ah, now you have moved it before \listoffigures. But this still can fail. So my initial comment is still valid. BTW: Adding \addcontentsline to \listfigurename is also not a good idea, e.g., if you are using \pagestyle{headings}. If you really, really don't want to use a package, the only workaround without redefining \listoffigures would be to use something like \addtocontents{lof}{\protect\addcontentsline{toc}{section}{\listfigurename}}. – cabohah Dec 07 '23 at 15:33
  • Another workaround would be to use needspace. But because this also needs a package it would make more sense to use tocbibind or my solution with tocbasic. – cabohah Dec 07 '23 at 15:42