2

I am using the KOMA script class scrreprt and would like to change the standard document font. But I noticed inconsistent font styles in the caption of listing-environments.

The following is a minimum working example.

\documentclass[]{scrreprt}

\usepackage{minted}

% the following 3 lines change the font and were copied from here: https://tug.org/FontCatalogue/sansmathfonts/
\usepackage{sansmathfonts}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\sfdefault} %% Only if the base font of the document is to be sans serif

\begin{document}

This is a test document. 

\begin{listing}
\begin{minted}{C}
    int main(){
        printf("hello world\n");
        return 0;
    }
\end{minted}
\caption{Hello World in C}
\end{listing}

\end{document}

This is the output:

"Listing 1" has a different font than the rest of the caption

As you can see, "Listing 1" has a different font than the rest of the caption.

I already had a look into the KOMA documentation and the closest thing I found was section "3.6 Text Markup" (2) but I couldn't find a solution. A guide by Overleaf (3) also didn't help me much.

My questions are:

  1. Am I doing something fundamentally wrong changing the font? Is there a best practice?
  2. How do I fix the different font in the caption?

Thank you very much in advance!

2 Answers 2

1

This is not a KOMA-Script issue but of the default style of package float, which is used by minted to define the listing environment and format the corresponding caption. But indeed KOMA-Script provides a solution for it: the float styles komaabove and komabelow for captions above or below the floating content, e.g.:

\documentclass[]{scrreprt}

\usepackage{float}% load package float to
\floatstyle{komabelow}% set the float style before
\usepackage{minted}% loading minted


% the following 3 lines change the font and were copied from here: https://tug.org/FontCatalogue/sansmathfonts/
\usepackage{sansmathfonts}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\sfdefault} %% Only if the base font of the document is to be sans serif

\begin{document}

This is a test document. 

\begin{listing}
\begin{minted}{C}
    int main(){
        printf("hello world\n");
        return 0;
    }
\end{minted}
\caption{Hello World in C}
\end{listing}

\end{document}

or

\documentclass[]{scrreprt}

\usepackage{minted}
\floatstyle{komabelow}% use float style komabelow for the already defined
\restylefloat{listing}% float type "listing"

% the following 3 lines change the font and were copied from here: https://tug.org/FontCatalogue/sansmathfonts/
\usepackage{sansmathfonts}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\sfdefault} %% Only if the base font of the document is to be sans serif

\begin{document}

This is a test document. 

\begin{listing}
\begin{minted}{C}
    int main(){
        printf("hello world\n");
        return 0;
    }
\end{minted}
\caption{Hello World in C}
\end{listing}

\end{document}

both with result:

enter image description here

Using one of the float styles komaabove or komabelow with package float has the general advantage, that the caption for floats not defined using KOMA-Script's tocbasic but package float also use the KOMA-Script caption settings, e.g., \setkomafont{caption}{…} or \setkomafont{captionlabel}{…} or option caption=nooneline or \setcapindent etc.

3
  • Thank you for the answer! This actually led me to another solution: When I load minted the following way, everything works as intended: \usepackage[newfloat=true]{minted}
    – flgei
    Commented May 21 at 12:59
  • 1
    @flgei With option newfloat no longer float is used, but package newfloat, which has several advantages, i.e., out of the box better KOMA-Script compatibility, better caption compatibility etc. Unfortunately minted does not provide an option tocbasic to define the new float type using tocbasic. This could result in best KOMA-Script compatibility.
    – cabohah
    Commented May 21 at 13:03
  • @cabohah The newfloat package uses the tocbasic package (if loaded) for several things, so using newfloat=true should already give best KOMA-Script compatibility.
    – user2574
    Commented May 23 at 17:33
1

You can select \normalfont when typesetting the caption label.

\documentclass[]{scrreprt}

\usepackage{sansmathfonts}
\usepackage[T1]{fontenc}
\usepackage{minted}

\renewcommand*\familydefault{\sfdefault}
% change also the font for the caption label
\AddToHook{cmd/listingscaption/before}{\normalfont}

\begin{document}

This is a test document. 

\begin{listing}
%\begin{minted}{C}
%    int main(){
%        printf("hello world\n");
%        return 0;
%    }
%\end{minted}
\caption{Hello World in C}
\end{listing}

\end{document}

enter image description here

2
  • Thanks for the solution! But I'm still wondering, why changing the standard document font does not work as I intended. Is there a reason for this? Or is there no way around this "hack"?
    – flgei
    Commented May 21 at 12:06
  • 2
    @flgei I guess the problem is with float.
    – egreg
    Commented May 21 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .