ネストされたキャプションと番号が付いた規定のボックス図と同じ結果を得るためのコードをプリアンブルに作成する方法

ネストされたキャプションと番号が付いた規定のボックス図と同じ結果を得るためのコードをプリアンブルに作成する方法

一連の図を強調表示するために使用されるボックスを制御するために、\chapter、\section、\subsection に似たルーチン (つまり、#2、#1 パラメータを含むルーチン) を作成できますか? 以下の MWE には、最終結果がどのようになるかを示した例が含まれています。可能であれば、画像の長さ (幅) と番号付きキャプションをルーチンで自動的に検出できますか?

\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}

答え1

MWEは

ここに画像の説明を入力してください

何が問題なのか説明してください。ご返答後、この回答を削除します。

答え2

コード内で使用する色を数回変更しており、試行における innerbox と outerbox のオプションが同じではないため、正確に何を望んでいるのかはわかりませんが、これで目的に近づき、出発点が得られると思います。

title=オプションを使用しているものの、タイトルを使用したくないと思われるため、コードがうまく機能しません。

しかし、私はさらにコンパクトなルートをお勧めします。

内部に で作成された環境を持つカスタム コマンドを定義しても、\newtcolorbox特にうまく機能しません。\NewDocumentCommand内部に通常の tcolorbox 環境を使用することをお勧めします。定義済みのコマンド内にあるため、毎回オプションを個別に追加する必要はありません。コマンドを呼び出すたびに同じオプションが使用されます。

\myimagebox次のコードは、画像の名前、キャプション テキスト、相互参照に使用するラベルの 3 つの引数を取るコマンドを定義します。

画像とキャプションの幅を決定し、適切なサイズのボックスを作成します。

[オプションの 4 番目の引数を内部に渡すこともできますが、必須ではありません。]また、内部のコマンドに追加の引数を渡すことも\includegraphicsできます。これを使用すると、必要に応じて画像のサイズやその他のプロパティを手動で調整できます。

(この設定では、120mm 程度より大きい画像は、ボックスに他のボックスと同様のパディングを持たせる十分なスペースがないため、見栄えがよくありません。そのため、この調整を使用して画像サイズを調整する必要がある場合があります。それ以外の場合は、画像の自然なサイズを使用して、そこからボックスを計算します。)

\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}

私が追加したものはコメントのあたりから始まります% some lengths and a counter we'll need later

\frontmatter前文に意味がないので移動しました。

tcolorboxes のサンプル出力

関連情報