2

I'm writing my thesis in Latex and this is the first time ever for me to use it. I've figured out a lot of things except this one - I need custom captions for pictures that are connected with sections, subsections, etc. I've put this lines in my preamble

\usepackage{chngcntr}
\counterwithin{figure}{section}
\counterwithin{figure}{subsection}
\counterwithin{figure}{subsubsection}

but when I'm using images in section it looks like this: Image with subsubsection that doesn't match And it should look like this: The way it should look like

What I need is a way to keep this look in subsections and subsubsections, but not in sections.

Moirae
  • 295
  • Welcome to TeX.SX! Please 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}. – jub0bs Sep 12 '13 at 12:39
  • 2
    So if the figure appears at the top level of section 1 it should be numbered 1.1, but if it appears after subsection 1.1 has started it should be numbered 1.1.1? I don't think this is a good numbering system at all. – egreg Sep 12 '13 at 12:48

2 Answers2

5

We add the figure counter to the reset list of every relevant sectioning command; then we redefine \thefigure to check (from top to bottom) which is the lowest level non zero sectional counter. Finally, with the help of tocloft, we increase the space reserved for the figure numbers in the list of figures.

\documentclass{article}
\usepackage{chngcntr,tocloft}
\counterwithin*{figure}{section}
\counterwithin*{figure}{subsection}
\counterwithin*{figure}{subsubsection}

\addtolength{\cftfignumwidth}{2em}


\renewcommand{\thefigure}{%
  \ifnum\value{subsection}=0
    \thesection.\arabic{figure}%
  \else
    \ifnum\value{subsubsection}=0
      \thesubsection.\arabic{figure}%
    \else
      \thesubsubsection.\arabic{figure}%
    \fi
  \fi
}

\begin{document}
\listoffigures
\section{A section}
\begin{figure}[!htp]
\caption{section.figure}
\end{figure}
\subsection{A subsection}
\begin{figure}[!htp]
\caption{subsection.figure}
\end{figure}
\begin{figure}[!htp]
\caption{subsection.figure}
\end{figure}
\subsubsection{A subsubsection}
\begin{figure}[!htp]
\caption{subsubsection.figure}
\end{figure}
\subsection{A subsection}
\begin{figure}[!htp]
\caption{subsection.figure}
\end{figure}
\section{A section}
\begin{figure}[!htp]
\caption{section.figure}
\end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712
1

Edit: Now I understand the question! If you put:

\def\mych{\counterwithin{figure}{chapter}\chapter}
\def\mysec{\counterwithin{figure}{section}\section}
\def\mysubsec{\counterwithin{figure}{subsection}\subsection}

in the preamble, and use \mysec instead of \section etc. it should work. I'm sure you could extend it easily for equation and table numbering.

Archived answer:

\usepackage{chngcntr}
\counterwithin{figure}{section}

Is all you need. The other 2 lines are the problem, specifically they set the figure counter to the 3rd, then the 4th level.

Chris H
  • 8,705
  • unfortunately it doesn't work. It should be exactly like egreg wrote. If the figure appears at the top level of section 1 it should be numbered 1.1, but if it appears after subsection 1.1 has started it should be numbered 1.1.1

    The numbering system is not up to me, it is formatting rule for thesis at my University and I can't change it ;)

    – Moirae Sep 13 '13 at 14:41
  • @Moirae, OK, that's odd - really odd, which is why I assumed you meant something different. What happens if you change \counterwithin in the middle of a document? Can you even do it? I'll try a test myself. – Chris H Sep 13 '13 at 14:45
  • Thank you both for excellent solutions! Both of them work and are great help to me :) – Moirae Sep 13 '13 at 16:43