1

I am writing a paper for my college and I use the tcolorbox package. What I am trying to do is using simultaneously the tcbincludegraphics but with the tcbcounter, and not another counter. I want to automate the label so that I don't need to add the label every time I add a tcbincludegraphics. This shoud be confusing for you. Here is a MWE :

\documentclass[11pt, a4paper, dvipsnames]{book}

\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[many]{tcolorbox}
\usepackage{xcolor}

\newcommand{\myfigure}[2][]{\tcbincludegraphics[title=Figure~{#1}]{#2}}

\begin{document}

\myfigure[test]{latex.png}

\myfigure[test 2]{latex.png}

\end{document}

What I am looking for in this MWE is something like "Figure 3.5 - Test" which includes the counter and labels so I can make a reference like \ref{fig:test} and LaTeX give me something like "Figure 3.5". I am looking for an automatic label in the preamble which take the name #1 so that I am not forced to do every time label{fig:test} and label{fig:test 2}etc.

Thank you in advance ^^

2
  • 1
    tcbincludegraphics cannot include an automatic counter. I think it's better that you use a regular tcolorbox with blend into=figures and all particularities that you want to use.
    – Ignasi
    Mar 27, 2022 at 16:31
  • @Ignasi Thank you but have you got an idea to make the size of the graphic automatically adjust to the size of the tcolorbox like with tcbincludegraphics instead of entering the height each time?
    – Dorian
    Mar 27, 2022 at 21:59

2 Answers 2

2

As I understand, you are not looking for the standard figure counter. The following code uses a new counter mycounter and also uses #1 for title and label.

Since your first parameter is optional, I also added a string test from toolbox to check, if #1 is present.

\documentclass[11pt, a4paper, dvipsnames]{book}

\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[many]{tcolorbox}
\usepackage{xcolor}
\usepackage{etoolbox}

\newcounter{mycounter}[chapter]
\renewcommand{\themycounter}{\thechapter.\arabic{mycounter}}

\newcommand{\myfigure}[2][]{%
  \ifstrempty{#1}
    {%
      \tcbincludegraphics{#2}%
    }%
    {%
      \tcbincludegraphics[%
          step and label={mycounter}{fig:#1},
          title=Figure~\themycounter:~{#1},
        ]%
        {#2}%
    }%
}

\begin{document}

\chapter{Test}

See \ref{fig:test} and \ref{fig:test 2}.

\myfigure[test]{example-image}

\myfigure[test 2]{example-image-a}

\myfigure{example-image-b}    

\end{document}

Update 2022-03-30:

The following code uses figure as counter to blend into normal figures. Also, I suggest a macro \figref to include Figure for references instead of manipulation \the...

\documentclass[11pt, a4paper, dvipsnames]{book}

\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[many]{tcolorbox}
\usepackage{xcolor}
\usepackage{etoolbox}

\newcommand{\myfigure}[2][]{%
  \ifstrempty{#1}
    {%
      \tcbincludegraphics{#2}%
    }%
    {%
      \tcbincludegraphics[%
          step and label={figure}{fig:#1},
          title=Figure~\thefigure:~{#1},
          phantom={\addcontentsline{lof}{section}{\protect\numberline{\thefigure}{#1}}},
        ]%
        {#2}%
    }%
}

\DeclareRobustCommand{\figref}[1]{Figure~\ref{fig:#1}}

\begin{document}

\listoffigures

\chapter{Test}

See \figref{test} and \figref{test 2}.

\myfigure[test]{example-image}

\myfigure[test 2]{example-image-a}

\myfigure{example-image-b}

\end{document}
6
  • Well, first thank you. But this is not exactly what I am looking for To be precise what I'm looking for is: A tcolorbox exactly like tcbincludegraphics (i.e. with the graphic fitting the width of the tcolorbox and not the other way around) but :
    – Dorian
    Mar 28, 2022 at 15:56
  • _ I want it to be numbered (with the figure counter or not I don't care): you solved it, thx _ I want to be able to make references without needing to add the label each time: you solved it, thx _ When making a reference, I want the render to be "Figure 1.1" and not just "1.1" _ Finally, the reason why I wanted the tcbcounter was that thanks to that I could easily insert a table of additional figures, but now I don't know how to do it with your new method.
    – Dorian
    Mar 28, 2022 at 15:56
  • Ok, for "Figure 1.1" instead of "1.1" I solved it : I erased the "Figure" in the title and i put "~Figure" here : \renewcommand{\themycounter}{Figure~\thechapter.\arabic{mycounter}} can you just help me to do the table of figures please :)
    – Dorian
    Mar 28, 2022 at 16:00
  • Can you help me to create a table of figures please :)
    – Dorian
    Mar 29, 2022 at 16:49
  • 1
    First, the graphic is fitting the width of the tcolorbox... I updated my answer with blending into figure to make use of \listoffigures for the table of figures. Mar 30, 2022 at 17:10
1

capture=hbox option automatically adjusts tcolorbox size to their contents, and blend into=figures consider tcolorboxes as floats and associated to figures counter. This option cannot be used with tcbincludegraphics, but Thomas F. Sturm provided an alternative solution.

\documentclass[11pt, a4paper, dvipsnames]{book}

\usepackage[T1]{fontenc}
\usepackage[many]{tcolorbox}

\newtcolorbox[blend into=figures]{myfig}[2][]{%
    float=htb, capture=hbox, title=#2, label={fig:#2}, 
    every float=\centering, #1
}

\begin{document}

\begin{myfig}{example}
\includegraphics[width=5cm]{example-image}
\end{myfig}

\begin{myfig}{example-b}
\includegraphics[width=4cm]{example-image-b}
\end{myfig}

As you can see in Fig~\ref{fig:example} and Fig~\ref{fig:example-b} \dots

\end{document}

enter image description here

1
  • Thank you but I want the tcolorbox to take the full length of the page and I want the image size to adapt to the size of the tcolorbox (and therefore the page). Btw Thomas F. Sturm (the tcolorbox developer) already almost solved my problem, but thank you :)
    – Dorian
    Mar 28, 2022 at 16:07

You must log in to answer this question.

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