Graphics, Figures & Tablessubcaption, floats, captionof and captionabove: Koma-Script

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Hoed
Posts: 28
Joined: Sat Jan 07, 2012 12:46 pm

subcaption, floats, captionof and captionabove: Koma-Script

Post by Hoed »

Hi there,

I am trying to cope with a long-known bug in Koma-script (see: https://tex.stackexchange.com/questions ... r-subfloat, which is from 2013.) Before I start introducing my problem, let me give you the question I have (as I certainly would not recognise the question at the end of this...)

How can I solve the numbering issue in the non-floating environment?

When I want to number and reference subfigures correctly, a bug in Koma-script makes this impossible without a work-around. To test this try:

Code: Select all

 \documentclass[paper=A4]{scrbook}
 
 \usepackage{scrhack}
 
 \usepackage[ngerman]{babel} %Sprachpaket
 \usepackage[T1]{fontenc} %Sonderzeichen, Umlaute
 \usepackage[latin9]{inputenc} %Sonderzeichen, Umlaute
 
 \usepackage[tableposition=top,figureposition=top]{caption}
 \DeclareCaptionFont{nicered}{\color{nicered}}
 \captionsetup{labelfont={nicered,bf},textfont={nicered,bf}}
 \usepackage{subcaption}
 \renewcommand\thesubfigure{\alph{subfigure})}
 \DeclareCaptionLabelFormat{opening}{(#2}
 \captionsetup[subfigure]{labelformat=opening}
 
 \usepackage[demo]{graphicx}
 \usepackage{tikz} 
 \usetikzlibrary{arrows,automata,calc,shapes,patterns,decorations,positioning,shadows,intersections,3d,decorations.pathreplacing} %TIKZ-Options
 \usepackage{calc}
 \usepackage{fp}
 \usepackage{pgfplots}
 \pgfplotsset{compat=1.9}
 
 \usepackage{txfonts}
 \usepackage{soul}
 \usepackage{url}
 \definecolor{nicered}{rgb}{.647,.129,.149}

\begin{document}
	
	\begin{figure}[htb!]
		\centering
		\caption{Fig 1}
		\label{fig:1}
		\begin{subfigure}{5.5cm}
		\caption{Fig 1.1}
		\label{fig:1:1}
		\includegraphics[width=4cm]{name}	
		\end{subfigure}
		\begin{subfigure}{5.5cm}
		\caption{Fig 1.2}
		\label{fig:1:2}
		\includegraphics[width=4cm]{name}	
		\end{subfigure}
	\end{figure}
	
	\begin{figure}[htb!]
		\centering
		\caption{Fig 2}
		\label{fig:2}
		\begin{subfigure}{5.5cm}
			\caption{Fig 2.1}
			\label{fig:2:1}
			\includegraphics[width=4cm]{name}
		\end{subfigure}
		\begin{subfigure}{5.5cm}
			\caption{Fig 2.2}
			\label{fig:2:2}
			\includegraphics[width=4cm]{name}	
		\end{subfigure}
	\end{figure}
	
	Verweise zu den Abbildungen: 
	
	\ref{fig:1} 
	
	\ref{fig:1:1} 
	
	\ref{fig:1:2} 
	
	\ref{fig:2} 
	
	\ref{fig:2:1} 
	
	\ref{fig:2:2}

\end{document}
and, for correct numbering, try this:

Code: Select all

\begin{figure}[htb!]
		\centering
		\captionabove{Fig 1}
		\label{fig:1}
		\begin{subfigure}{5.5cm}
		\caption{Fig 1.1}
		\label{fig:1:1}
		\includegraphics[width=4cm]{name}	
		\end{subfigure}
		\begin{subfigure}{5.5cm}
		\caption{Fig 1.2}
		\label{fig:1:2}
		\includegraphics[width=4cm]{name}	
		\end{subfigure}
	\end{figure}
	
	\begin{figure}[htb!]
		\centering
		\captionabove{Fig 2}
		\label{fig:2}
		\begin{subfigure}{5.5cm}
			\caption{Fig 2.1}
			\label{fig:2:1}
			\includegraphics[width=4cm]{name}
		\end{subfigure}
		\begin{subfigure}{5.5cm}
			\caption{Fig 2.2}
			\label{fig:2:2}
			\includegraphics[width=4cm]{name}	
		\end{subfigure}
	\end{figure}
in the same document setting. I only changed

Code: Select all

\caption
to

Code: Select all

\captionabove
in the main-caption of the figures.

The problem arises, when I want to put the subfigures in a non-floating environment and thereby use

Code: Select all

\captionof
to caption the figures. I can't use

Code: Select all

\captionabove
anymore. The following MWE shows what happens to nummeration and references without

Code: Select all

\captionabove
in a non-floating environment.

Code: Select all

\documentclass[paper=A4]{scrbook}
 
 \usepackage{scrhack}
 
 \usepackage[ngerman]{babel} %Sprachpaket
 \usepackage[T1]{fontenc} %Sonderzeichen, Umlaute
 \usepackage[latin9]{inputenc} %Sonderzeichen, Umlaute
 
 \usepackage[tableposition=top,figureposition=top]{caption}
 \DeclareCaptionFont{nicered}{\color{nicered}}
 \captionsetup{labelfont={nicered,bf},textfont={nicered,bf}}
 \usepackage{subcaption}
 \renewcommand\thesubfigure{\alph{subfigure})}
 \DeclareCaptionLabelFormat{opening}{(#2}
 \captionsetup[subfigure]{labelformat=opening}
 
 \usepackage[demo]{graphicx}
 \usepackage{tikz} 
 \usetikzlibrary{arrows,automata,calc,shapes,patterns,decorations,positioning,shadows,intersections,3d,decorations.pathreplacing} %TIKZ-Options
 \usepackage[framemethod=tikz]{mdframed}
 \usepackage{calc}
 \usepackage{fp}
 \usepackage{pgfplots}
 \pgfplotsset{compat=1.9}
 
 \usepackage{txfonts}
 \usepackage{soul}
 \usepackage{url}
 
 \newcounter{Beispiel}[section]
 \newenvironment{Beispiel}[1][]{%
 	\refstepcounter{Beispiel}%
 	\ifstrempty{#1}%
 	{\mdfsetup{%
 			frametitle={%
 				\tikz[baseline=(current bounding box.east),outer sep=0pt]
 				\node[line width=1pt,anchor=east,rectangle,draw=nicered,fill=redtitleback,general shadow={shadow scale=1, shadow xshift=0.8ex, shadow yshift=-0.8ex,
 					opacity=1, fill=gray!50}]
 				{\strut \color{nicered}{Beispiel}~\theBeispiel};}}
 	}%
 	{\mdfsetup{%
 			frametitle={%
 				\tikz[baseline=(current bounding box.east),outer sep=0pt]
 				\node[line width=1pt,anchor=east,rectangle,draw=nicered,fill=redtitleback, general shadow={shadow scale=1, shadow xshift=0.8ex, shadow yshift=-0.8ex,
 					opacity=1, fill=gray!50}]
 				{\strut \color{nicered}{Beispiel}~\theBeispiel:~\color{nicered}{#1}};}}%
 	}%
 	\mdfsetup{roundcorner=10pt,innertopmargin=10pt,linecolor=nicered,%
 		linewidth=1pt,topline=true,shadow=true,%
 		frametitleaboveskip=\dimexpr-\ht\strutbox\relax,}
 	\begin{mdframed}[]\relax%
 	}{\end{mdframed}}
 
 \DeclareCaptionType[fileext=ext]{float}

 
 
 \definecolor{nicered}{rgb}{.647,.129,.149}
 \definecolor{redtitleback}{cmyk}{0,0.1,0.1,0}

\begin{document}
	
	\begin{figure}[htb!]
		\centering
		\captionabove{Fig 1}
		\label{fig:1}
		\begin{subfigure}{5.5cm}
		\caption{Fig 1.1}
		\label{fig:1:1}
		\includegraphics[width=4cm]{name}	
		\end{subfigure}
		\begin{subfigure}{5.5cm}
		\caption{Fig 1.2}
		\label{fig:1:2}
		\includegraphics[width=4cm]{name}	
		\end{subfigure}
	\end{figure}
	
	\begin{figure}[htb!]
		\centering
		\captionabove{Fig 2}
		\label{fig:2}
		\begin{subfigure}{5.5cm}
			\caption{Fig 2.1}
			\label{fig:2:1}
			\includegraphics[width=4cm]{name}
		\end{subfigure}
		\begin{subfigure}{5.5cm}
			\caption{Fig 2.2}
			\label{fig:2:2}
			\includegraphics[width=4cm]{name}	
		\end{subfigure}
	\end{figure}
	
	
	Verweise zu den Abbildungen: 
	
	\ref{fig:1} 
	
	\ref{fig:1:1} 
	
	\ref{fig:1:2} 
	
	\ref{fig:2} 
	
	\ref{fig:2:1} 
	
	\ref{fig:2:2}
	
	\newpage
			\begin{Beispiel}[Test 1]
			\label{bsp:1}
						
			\captionof{figure}{Fig 3}
			\label{fig:3}
			\begin{subfigure}{5.5cm}
				\caption{Fig 3.1}
				\label{fig:3:1}
				\includegraphics[width=4cm]{name}
			\end{subfigure}
			\begin{subfigure}{5.5cm}
				\caption{Fig 3.2}
				\label{fig:3:2}
				\includegraphics[width=4cm]{name}
			\end{subfigure}
	\end{Beispiel}
		

Verweise zu den Abbildungen:	

\ref{fig:3} 

\ref{fig:3:1} 

\ref{fig:3:2}	

\end{document}
Compiling this leads to correct numbering in the figure-environment and incorrect numbering in the non-floating "Beispiel"-environment (which was not build by me!), because the subfigures are regarded as standalone-figures.

If you have any hint where to start...

Thanks. H.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

subcaption, floats, captionof and captionabove: Koma-Script

Post by Johannes_B »

Have you checked if this bug is still valid?
I don't see any problem at all. You just have to call captions=figureheading as already mentioned by Axel on TeX.SX.

Code: Select all

\documentclass[paper=A4,captions=figureheading]{scrbook}

\usepackage{scrhack}

\usepackage[ngerman]{babel} %Sprachpaket
\usepackage[T1]{fontenc} %Sonderzeichen, Umlaute
\usepackage[latin9]{inputenc} %Sonderzeichen, Umlaute

\usepackage[tableposition=top,figureposition=top]{caption}
\usepackage{subcaption}
\renewcommand\thesubfigure{\alph{subfigure})}
\DeclareCaptionLabelFormat{opening}{(#2}
\captionsetup[subfigure]{labelformat=opening}

\usepackage[demo]{graphicx}

\begin{document}

\begin{figure}[htb!]
	\centering
	\caption{Fig 1}
	\label{fig:1}
	\begin{subfigure}{5.5cm}
		\caption{Fig 1.1}
		\label{fig:1:1}
		\includegraphics[width=4cm]{name}      
	\end{subfigure}
	\begin{subfigure}{5.5cm}
		\caption{Fig 1.2}
		\label{fig:1:2}
		\includegraphics[width=4cm]{name}      
	\end{subfigure}
\end{figure}

\begin{figure}[htb!]
	\centering
	\caption{Fig 2}
	\label{fig:2}
	\begin{subfigure}{5.5cm}
		\caption{Fig 2.1}
		\label{fig:2:1}
		\includegraphics[width=4cm]{name}
	\end{subfigure}
	\begin{subfigure}{5.5cm}
		\caption{Fig 2.2}
		\label{fig:2:2}
		\includegraphics[width=4cm]{name}      
	\end{subfigure}
\end{figure}

Verweise zu den Abbildungen:

\ref{fig:1}

\ref{fig:1:1}

\ref{fig:1:2}

\ref{fig:2}

\ref{fig:2:1}

\ref{fig:2:2}

\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Hoed
Posts: 28
Joined: Sat Jan 07, 2012 12:46 pm

subcaption, floats, captionof and captionabove: Koma-Script

Post by Hoed »

Honestly: I didn't follow Axel's advise, because he stated: "Doesn't work" Bug is solved, it works with setting

Code: Select all

captions=figureheading
Shame on me. Next time, I'll leave a problem alone for some time before searching again and post only afterwards.

Thank you very much.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

subcaption, floats, captionof and captionabove: Koma-Script

Post by Johannes_B »

By the way, KOMA also provides commands for non-floating objects, as you can read in the documentation.

Code: Select all

\documentclass[paper=A4,captions=figuresignature,captions=tableheading]{scrbook}
\begin{document}

\begin{center}
	\begin{minipage}{.5\textwidth}
		\rule{\linewidth}{5cm}
		\captionbelowof{figure}{This is a figure caption}
	\end{minipage}
\end{center}
\begin{center}
	\begin{minipage}{.5\textwidth}
		\captionaboveof{table}{This is a table caption}
		\rule{\linewidth}{5cm}
	\end{minipage}
\end{center}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

subcaption, floats, captionof and captionabove: Koma-Script

Post by sommerfee »

First of all Johannes informed me about the obsolete character of my answer on SX regarding KOMA-Script, and I just updated that. Sorry for the confusion I have caused by forgetting updating the answer on SX.

Even if the problem is solved now I would like to add one additional note:
Hoed wrote:The problem arises, when I want to put the subfigures in a non-floating environment and thereby use

Code: Select all

\captionof
to caption the figures. I can't use

Code: Select all

\captionabove
anymore.
There is a way to use \captionabove anyway, use \captionsetup{type=figure} right at the beginning of your own non-floating environment. This has an advantage over using \captionof: Hyperlinks will jump to the beginning of the environment (and not to the caption) since \captionsetup{type=figure} will set a hyperlink anchor when used with hyperref.
Post Reply