3

Below is my full LaTeX document.

My goal is to create two different caption styles. One style for the caption for the programming blocks (created with listings), where above the programming block, a caption with a grey background and black outline appears. This is done in \DeclareCaptionStyle{style1}.

Then I want a second caption style which has no style applied to it and is just used below images. This is done in \DeclareCaptionStyle{style2}.

Right now, the style for the programming blocks (\DeclareCaptionStyle{style1}) is not being applied in the document. I am not sure what is wrong with my syntax.

\documentclass[12pt,dvipsnames]{article}
\usepackage{graphicx, listings, caption}
\usepackage[most]{tcolorbox}
\graphicspath{{./Images/}}

\tcbset{on line,
  boxsep=4pt, left=0pt,right=0pt,top=0pt,bottom=0pt,
  colframe=black,colback=lightgray,
  highlight math style={enhanced}
}

\DeclareCaptionStyle{style1}{
  font={sf,small},
  box=colorbox,boxcolor=gray,
  labelformat=empty,
  justification=raggedright,
  singlelinecheck=false
}
\DeclareCaptionStyle{style2}{
}
\captionsetup[R]{style=style1}
\captionsetup[graphics]{style=style2}

\lstset{
  rulecolor=\color{Black},
  backgroundcolor=\color{White},
  breaklines=true,
  tabsize=4,
  showstringspaces=false
  extendedchars=\true,
  frame=single,
  framesep=3pt,
  framerule=0.4pt,
  xleftmargin=3.4pt,
  xrightmargin=3.4pt,
}

\lstdefinestyle{R}{
  language=R,
  caption={[R]{R}},
  basicstyle=\scriptsize\color{Gray}\ttfamily,
  keywordstyle=\color{Blue},
  identifierstyle=\color{Black},
  stringstyle=\color{BrickRed},
  commentstyle=\color{Green}
}

\begin{document}
\begin{lstlisting}[style=R]
print("hello")
\end{lstlisting}

\begin{figure}[h]
\centering
\includegraphics{imagename}
\caption[graphics]{Amazing captioning text.}
\end{figure}
\end{document}
6
  • The optional argument of \caption is for the "short caption", not the \captionsetup. You need to create a new float type for graphics, or just call \captionsetup each time. Commented Nov 22, 2023 at 14:03
  • @JohnKormylo Can you give an example of what you mean by creating a new float type for graphics?
    –  kyarudata
    Commented Nov 22, 2023 at 14:31
  • I seem to have found an easy solution to this. Changing to \captionsetup[lstlisting]{style=style1} and \captionsetup[figure]{style=style2} appears to change the setup globally. I wasn't aware that I could directly link to the lstlisting and figure but that seems to be the easiest fix with the intended result
    –  kyarudata
    Commented Nov 22, 2023 at 15:32
  • @JohnKormylo Will you answer?
    – cfr
    Commented Nov 22, 2023 at 16:55
  • @cfr - Nah. He is alreacy using two different float types, so no new float type needed. Besides, it would have used a diferent counter than figure. Commented Nov 22, 2023 at 18:32

1 Answer 1

1

Changing \captionsetup to refer to lstlisting and figure did the trick.

\captionsetup[lstlisting]{style=style1}
\captionsetup[figure]{style=style2}

You must log in to answer this question.

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