-1

Can a routine be created - similar to those for \chapter, \section, \subsection i.e., containing a number of #2, #1 parameters - to control the boxes used to highlight a series of figures? In my MWE below, I include an example of how I would like the end result to look. If possible, could the lengths (widths) of the images, and numbered captions be automatically detected by the routine?

\documentclass[english,11pt]{book}
%
\usepackage[fleqn]{amsmath} %
\usepackage{amsthm,amssymb} %
\usepackage[explicit]{titlesec}
\usepackage{titletoc} %
\usepackage[english]{babel} %
\usepackage{xcolor} %
\usepackage{colortbl} %
\usepackage{graphicx}
\usepackage{tikz}
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage{caption} %
\usepackage[margin=6pt,font+=smaller,labelformat=parens,labelsep=space,skip=2.4pt,list=false,hypcap=false]{subcaption}
\usepackage[paperwidth=170mm,paperheight=240mm,bindingoffset=12.5mm,left=12.5mm,right=12.5mm,top=8mm,bottom=8mm,showframe,showcrop]{geometry}
\usepackage[a4,frame,cam,center]{crop} %
\usepackage{newtxtext} %
\usepackage{cellspace}
\usepackage{mwe} %
\usepackage{refcount} %
\usepackage{hyperref}

\frontmatter

\addto\captionsenglish{
\renewcommand{\figurename}{Fig\hspace{0.8pt}.}
\renewcommand{\thefigure}{\thechapter\hspace{1.0pt}.\hspace{0.6pt}\arabic{figure}} }

\newtcolorbox{sectionbox}[2][]{nobeforeafter, boxsep=4.4pt,boxrule=1.6pt,arc=3pt,outer arc=3.2pt,
width=\linewidth, coltitle=white,colbacktitle=red!60!black,after=\hfill,colframe=black,colback=white,
title={#2},fonttitle=\bfseries,#1} %

\titleformat{\section}{}{}{0pt} %
{\begin{sectionbox}
{\fontsize{16}{18}\selectfont{Section\hspace{5.6pt}\thetitle}}
{\normalfont\fontsize{12}{12.8}\selectfont{#1}\vspace{-2.4pt}} %
\end{sectionbox}}

\renewcommand{\thesection}{\arabic{section}} %
\titlespacing{\section}{0pt}{15.35pt}{4pt}[0pt] % from left, before, after, [0pt]


\newtcolorbox[]{outerbox}[2][]
{boxrule=1.6pt,arc=1.2pt,coltitle=red!50!black,colbacktitle=pink,colback=gray,colframe=red,
every float=\centering,title={#2},#1}

\newtcolorbox[]{innerbox}[2][]
{boxsep=2pt,boxrule=0.7pt,arc=2pt,coltitle=red!50!black,colbacktitle=gray,
colback=white,colframe=black,fonttitle=\bfseries, title={#2},#1}


\begin{document}

\setcounter{chapter}{4}
\chapter{Chapter $\thechapter$}
\section{Using my graphic - please edit example-image-a} % my image=127.5mm wide

\begin{figure}[htp]
\begin{outerbox}[width=\textwidth-2mm]
\noindent\includegraphics[scale=1.00]{../math_text/chap_01_intro_to_number_figs/fig_40_step_forward}
%\noindent\includegraphics[width=45mm]{example-image-a}
\begin{innerbox}[width=80mm]
\caption{ - \hspace{0.6pt}using the preamble - gives errors}
\end{innerbox}
\end{outerbox}
\label{fig:a_figure}
\end{figure}

\begin{figure}[htp]
\centering
     \begin{tcolorbox}
[width=131mm,colframe=red!50!black,colback=gray,top=0pt,bottom=0pt,left=1.0pt,right=1.0pt,boxrule=1.6pt]
\centering
\noindent\includegraphics[scale=1.00]{../math_text/chap_01_intro_to_number_figs/fig_40_step_forward}
%\noindent\includegraphics[width=45mm]{example-image-a}
        \begin{tcolorbox}[center,width=87mm,height=7.2mm,top=1pt,left=1pt,right=1pt,colframe=black,colback=white,boxrule=0.7pt]
                    \caption{- \hspace{0.6pt}using direct coding after begin document}
        \end{tcolorbox}
    \end{tcolorbox}
    \label{fig:another_figure}
\end{figure}

\end{document}
5
  • 5
    You're much more likely to get an answer if you actually ask a question rather than just post some code without explanation. Also bear in mind none of us can compile that code without the image called in the figure. Use a sample image if you can instead.
    – frabjous
    May 13, 2022 at 20:32
  • 2
    As is, I'm not sure what you're asking, but it may be more obvious to me if I knew the dimensions of the image. For what I linked to, all you have to do is change "../math_text/chap_01_intro_to_number_figs/fig_08_five_and_three_merged" to "example-image-a". If the dimensions are important to your question, also change the "scale=" and/or add "height=100pt"/"width=200pt" (or whatever values) to the \includegraphics command until it exhibits the behavior you are unhappy with.
    – frabjous
    May 14, 2022 at 14:18
  • @frabjous Thanks for your guidance. Using the example-image-a appears to have suppressed the outer tcolorbox. My hope is to create a preamble entry that gives the same result as the given graphics arrangement - using three calling parameters: the image, the title and the title \hyperef number.
    – keith77777
    May 14, 2022 at 15:29
  • 1
    When I compile it with example-image-a, there's a tcolorbox in "ThemeGrey" (which isn't actually grey, but never mind that); the image is inside that, and below that is the caption in another yellow tcolorbox. I take it you don't want to manually specify the height and width each time, but what counts as "the same"? Should each one be the same size, and the image should scale? Or should each box grow or shrink with the width/height of the image/caption? If so, with how much padding? You can add images to your question if you think it would help explain the problem.
    – frabjous
    May 14, 2022 at 18:15
  • (repost) @keith77777 Cannot compile, ../math_text/chap_01_intro_to_number_figs/fig_40_step_forward not found.
    – user202729
    May 19, 2022 at 14:53

2 Answers 2

0
+50

Your MWE gives

enter image description here

Please explain what is your problem! I will delete this answer after your response.

10
  • Looks like OP leave the "question" as comments in the code itself
    – user202729
    May 14, 2022 at 18:45
  • @keith77777, and where is this stated in your question? Your question is unclear and should be closed. Or, please edit it and clearly stated what is your problem.
    – Zarko
    May 15, 2022 at 11:39
  • @keith77777 Questions with bounty cannot be closed. Alternatively you can just edit the question.
    – user202729
    May 16, 2022 at 2:42
  • Please just edit the question and answer my questions in the comments about what counts as "the same". What should the process of generating the widths/lengths look like? Should the boxes always be the same size or not? If not, what should determine how they change?
    – frabjous
    May 16, 2022 at 15:33
  • No, you really haven't tried to answer my questions. Read them again. There is still no question asked! I understand that the figures vary in width as do the captions but that does not explain how you want the boxes to be sized in relation to them! You seem to determined to make it impossible to help you.
    – frabjous
    May 17, 2022 at 16:48
0

I'm not sure exactly what you want, because you've changed the colors you use in your code a couple times, and the options for the innerbox and outerbox in your attempts are not the same, but I think this will get you close, and give you a place to start.

Your code does not work well in part because you're using the title= option but don't seem to want to use titles.

But I suggest going an even more compact route.

Defining custom commands that have, inside of them, environments created with \newtcolorbox does not work particularly well. I suggest just using \NewDocumentCommand with a regular tcolorbox environment inside; since it's inside a defined command, you don't need to add the options separately each time; the same ones will be used each time you call the command.

The following code defines a command \myimagebox which takes three arguments: the name of an image, the caption text, and a label to use for crossreferencing.

It determines the widths of the image and the caption and creates boxes of the appropriate sizes.

You can, but do not have to, pass it an optional fourth argument inside [ and ] as well if you want to pass additional arguments to the \includegraphics command inside. Using this you can manually tweak the size and other properties of the image if need be.

(With this setup, images larger than 120mm or so do not look good since there isn't enough room for the boxes to have similar padding to other boxes, so sometimes you may want to use this tweak the image size. In other cases, it will use the image's natural size and calculate the box from it.)

\documentclass[english,11pt]{book}
%
\usepackage[fleqn]{amsmath} %
\usepackage{amsthm,amssymb} %
\usepackage[explicit]{titlesec}
\usepackage{titletoc} %
\usepackage[english]{babel} %
\usepackage{xcolor} %
\usepackage{colortbl} %
\usepackage{graphicx}
\usepackage{tikz}
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage{caption} %
\usepackage[margin=6pt,font+=smaller,labelformat=parens,labelsep=space,skip=2.4pt,list=false,hypcap=false]{subcaption}
\usepackage[paperwidth=170mm,paperheight=240mm,bindingoffset=12.5mm,left=12.5mm,right=12.5mm,top=8mm,bottom=8mm,showframe,showcrop]{geometry}
\usepackage[a4,frame,cam,center]{crop} %
\usepackage{newtxtext} %
\usepackage{cellspace}
\usepackage{mwe} %
\usepackage{refcount} %
\usepackage{hyperref}


\addto\captionsenglish{
\renewcommand{\figurename}{Fig\hspace{0.8pt}.}
\renewcommand{\thefigure}{\thechapter\hspace{1.0pt}.\hspace{0.6pt}\arabic{figure}} }

\newtcolorbox{sectionbox}[2][]{nobeforeafter, boxsep=4.4pt,boxrule=1.6pt,arc=3pt,outer arc=3.2pt,
width=\linewidth, coltitle=white,colbacktitle=red!60!black,after=\hfill,colframe=black,colback=white,
title={#2},fonttitle=\bfseries,#1} %

\titleformat{\section}{}{}{0pt} %
{\begin{sectionbox}
{\fontsize{16}{18}\selectfont{Section\hspace{5.6pt}\thetitle}}
{\normalfont\fontsize{12}{12.8}\selectfont{#1}\vspace{-2.4pt}} %
\end{sectionbox}}

\renewcommand{\thesection}{\arabic{section}} %
\titlespacing{\section}{0pt}{15.35pt}{4pt}[0pt] % from left, before, after, [0pt]

% some lengths and a counter we'll need later
\newlength{\captionwidth}
\newlength{\innerboxwidth}
\newlength{\imagewidth}
\newlength{\outerboxwidth}
\newcounter{fakefigure}

\NewDocumentCommand{\myimagebox}{mmmo}{
    % a fake counter to use to calculate width of caption
    % which will be one more than current
    \setcounter{fakefigure}{\value{figure}}
    \stepcounter{fakefigure}
    % determine width of caption
    \settowidth{\captionwidth}{Fig.~\thefakefigure: #2}
    % determine innerbox size by adding to captionwidth
    \setlength{\innerboxwidth}{\captionwidth}
    \addtolength{\innerboxwidth}{10mm}
    % determine width of image
    \IfValueTF{#4}{%
        \settowidth{\imagewidth}{\includegraphics[#4]{#1}}%
    }{%
        \settowidth{\imagewidth}{\includegraphics{#1}}%
    }%
    % outerbox determined by max of those two widths
    \setlength{\outerboxwidth}{\maxof{\innerboxwidth}{\imagewidth}}
    \addtolength{\outerboxwidth}{20mm}
    % but not above \textwidth
    \setlength{\outerboxwidth}{\minof{\outerboxwidth}{\textwidth}}
    \begin{figure}[htp]%
        \centering%
        % outerbox
        \begin{tcolorbox}[
            center,
            width=\outerboxwidth,
            coltitle=red!50!black,
            colframe=red!50!black,
            colbacktitle=pink,
            colback=gray,
            top=0pt,
            bottom=0pt,
            every float=\centering
        ]%
            \centering%
            \IfValueTF{#4}{%
                \includegraphics[#4]{#1}%
            }{%
                \includegraphics{#1}%
            }%
            % innerbox
            \begin{tcolorbox}[center,top=1pt,bottom=-10pt,left=1pt,right=1pt,%
                width=\innerboxwidth,
                colframe=black,colback=white,boxrule=0.7pt]%
            \caption{#2}\label{#3}
            \end{tcolorbox}
        \end{tcolorbox}
    \end{figure}
}

\usepackage{lipsum}% for dummy text

\title{Document Title}
\author{Someone P. Someone}
\date{\today}

\begin{document}
\frontmatter
\maketitle

\mainmatter

\setcounter{chapter}{4}
\chapter{Chapter \thechapter}
\section{Using my graphic - please edit example-image-a} % my image=127.5mm wide

\myimagebox{sample100mm.png}{An 100mm example}{fig:samp1}

\lipsum[1]% dummy text

\myimagebox{sample50mm.png}{A 50mm example}{fig:samp2}

\lipsum[9]

\myimagebox{example-image-a}{This is my example with a longer caption}{fig:ex1}[width=50mm]

\lipsum[2]% dummy text

As seen in figures \ref{fig:samp1} and \ref{fig:ex1} ...

\end{document}

The stuff I added starts around the comment % some lengths and a counter we'll need later.

I moved \frontmatter because it doesn't make any sense in the preamble.

sample output for tcolorboxes

8
  • To answer questions under the other answer: The reason I specify [width=50mm] is that example-image-a is too big otherwise; I already explained the need for such tweaking in my answer. If the images are already a good size, you don't need that at all, and you can use a different size if you prefer, or scale= instead of width=. You should be able to change the second argument after \addtolength without losing centering so long as the values are reasonable. If not, give an example. I don't understand why printing the values would help anything, but where you want them printed?
    – frabjous
    May 18, 2022 at 14:52
  • No, in my code, changing 10mm to 18mm absolutely does not offset anything. You're making other changes, or using different images, and not saying what they are. Post an example, as I asked, using example-image-a if possible. If the problem is only for your images, give their dimensions, in pixels. You can print lengths with the printlen package and its command \printlength, but the lengths are only defined in the environment, and are given in pts not mms. You still didn't say where you wanted them printed, as I asked. Please try to cooperate if you want help!
    – frabjous
    May 18, 2022 at 15:58
  • @keith77777 It appears that for whatever reason your attempt to communicate the question to other people has not been successful, and that will hinder people's attempt to solve your problem. You can edit the original question to show the updated code, its output, and explain what's wrong with the output/what do you want to be done automatically with the code.
    – user202729
    May 19, 2022 at 5:35
  • @user202729 I have edited my question - I hope that is adequate.
    – keith77777
    May 19, 2022 at 11:06
  • @user202729 Please move comments regarding the question unrelated to my answer to underneath the question. Thanks.
    – frabjous
    May 19, 2022 at 14:21

You must log in to answer this question.

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