tcolorbox 内での Tikz 画像の中央揃え

tcolorbox 内での Tikz 画像の中央揃え

この質問は私の最後の質問そして答え@frougon より。

回答のコードは自分のニーズに合わせて少し編集しましたが、重要な部分はそのまま残っています (そう願っています)。

私はimagesAltと を作成しましたquestionpictureAlt。これにより、enumerate環境が削除され、列挙を必要とせずに Tikzpictures をドキュメントに直接配置できるようになりました。これまでのところ、かなりうまく機能しています。

私が抱えている問題は、Tikzpictures がページの中央に配置されていないことです。これは、frougon の回答に対して行った編集によるものか、tcolorbox使用しているパッケージに関係するものなのか (そうではないと思います)、あるいは他の何かなのかはわかりません。

以下は私のコード/出力です。色付きの線が含まれており、各色の線の長さは同じで、適切に中央揃えされていないことを示しています。

\documentclass{article}
\usepackage{graphicx}
\usepackage{calc} % for \settototalheight (used for convenience)
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{parskip}
\usepackage{enumitem}
\usetikzlibrary{calc}


% Frame dimensions
\newlength{\mywidth}
\newlength{\myheight}
\setlength{\mywidth}{3.5cm}
\setlength{\myheight}{3.5cm}

\makeatletter

% l3keys supports a key/value-style interface for the optional argument of the 'images' environment.
\ExplSyntaxOn

\msg_new:nnn { form } { duplicate-figure-id }
  { duplicate~figure~identifier:~'\exp_not:n {#1}'. }

% Whether to perform the \scalebox-based autoscaling for a given figure
\bool_new:N \l__form_do_autoscale_pic_in_images_bool
% Sequence recording all figure identifiers (for the 'scale to max size' TikZ style) found so far
\seq_new:N \g__form_scale_to_max_style_figure_ids_seq
% Counter used when generating automatic figure identifiers for 'form autoscale'
\int_new:N \g_form_last_autogenerated_figure_nb_int

% Define the options supported in the optional argument of the 'images' environment
\keys_define:nn { form }
  {
    autoscale .bool_set:N = \l__form_do_autoscale_pic_in_images_bool,
    % Value used when the 'autoscale' key is passed with no value
    autoscale .default:n = { true },
    autoscale .initial:n = { false }
  }

\cs_new_protected:Npn \__form_set_keys:n #1
  { \keys_set:nn { form } {#1} }

\cs_new_protected:Npn \__form_check_unique_id:n #1
  {
    \seq_if_in:NnTF \g__form_scale_to_max_style_figure_ids_seq {#1}
      { \msg_error:nnn { form } { duplicate-figure-id } {#1} }
      { \seq_gput_right:Nn \g__form_scale_to_max_style_figure_ids_seq {#1} }
  }

\cs_new_protected:Npn \__form_form_autoscale:n #1
  {
    \pgfkeys { /tikz/.cd, form~autoscale={#1} }
  }

\cs_generate_variant:Nn \__form_form_autoscale:n { V }

% Automatic construction of generated ids (the pattern is defined here)
\cs_new:Npn \__form_autogenerated_id:n #1
  { form~autogenerated~id~#1 }

\cs_generate_variant:Nn \__form_autogenerated_id:n { V }

\cs_new_protected:Npn \__form_form_autoscale_autoid:
  {
    % Increment the counter
    \int_gincr:N \g_form_last_autogenerated_figure_nb_int
    \tl_set:Nx \l_tmpa_tl       % generate an ID based on the counter value
      {
        \__form_autogenerated_id:V
          \g_form_last_autogenerated_figure_nb_int
      }
    % Call the 'form autoscale' style with the new ID
    \__form_form_autoscale:V \l_tmpa_tl
  }

% Set up aliases using LaTeX2e naming style
\cs_set_eq:NN \formsetup \__form_set_keys:n
\cs_set_eq:NN \form@check@unique@id \__form_check_unique_id:n
\cs_set_eq:NN \form@form@autscale@autoid \__form_form_autoscale_autoid:

% If-then-else command using the boolean
% \l__form_do_autoscale_pic_in_images_bool to choose the branch
\NewDocumentCommand \form@ifautoscale@enabled { }
  {
    \bool_if:NTF \l__form_do_autoscale_pic_in_images_bool
  }

\ExplSyntaxOff

\newsavebox{\form@box}     % will be set with \global
\newlength{\form@total@height}

\newenvironment{questionpicture}{%
  % 'form autoscale autoid' should turned on for every picture framed by the 'images' environment
  \tikzset{every picture/.style=form autoscale autoid}%
  \begin{lrbox}{0}
}{%
  \end{lrbox}%
  \global\setbox\form@box=\box0
}

\AfterEndEnvironment{questionpicture}{%
  \par\noindent
  \setlength{\fboxsep}{0pt}%
  \begingroup
  \minipage{\dimexpr\linewidth-\mywidth-2\fboxrule-2\fboxsep}
  \begingroup
  \enumerate[label=(\Alph*), ref=\Alph*, align=left, itemsep=7pt]
}



%% questionpictureAlt is equivalent to questionpicture, without the enumerate environment. It is used for direct placement of an image.

\newenvironment{questionpictureAlt}{%
  \tikzset{every picture/.style=form autoscale autoid}%
  \begin{lrbox}{0}
}{%
  \end{lrbox}%
  \global\setbox\form@box=\box0
}

\AfterEndEnvironment{questionpictureAlt}{%
  \par\noindent
  \setlength{\fboxsep}{5pt}%
  \begingroup
  \minipage{\dimexpr\linewidth-\mywidth-2\fboxrule-2\fboxsep}
}

\let\form@start@question@picture\questionpicture
\newcommand*{\form@invalid@place@for@calling@questionpicture}{%
  \errmessage{The 'questionpicture' environment must be used at the start of a
    'images' environment}%
}
% Generate an error message unless \questionpicture is used where expected
\let\questionpicture\form@invalid@place@for@calling@questionpicture

\let\form@start@question@picture\questionpictureAlt
\newcommand*{\form@invalid@place@for@calling@questionpictureAlt}{%
  \errmessage{The 'questionpictureAlt' environment must be used at the start of a
    'images' environment}%
}
% Generate an error message unless \questionpictureAlt is used where expected
\let\questionpictureAlt\form@invalid@place@for@calling@questionpictureAlt


\newenvironment{images}[1][]{%
  \formsetup{#1}%
  \let\questionpicture\form@start@question@picture
  \ignorespaces
}{%
  \endenumerate
  \endgroup
  \endminipage
  \endgroup
  %
  \settototalheight{\form@total@height}{\usebox{\form@box}}%
  \pgfmathsetmacro{\form@x@ratio}{\the\mywidth / \wd\form@box}%
  \pgfmathsetmacro{\form@y@ratio}{\the\myheight / \form@total@height}%
  \pgfmathsetmacro{\form@scale}{min(\form@x@ratio,
                                         \form@y@ratio)}%
  %
    \begin{minipage}[c][\myheight][c]{\mywidth}
      \centering
      \form@ifautoscale@enabled{%
        \scalebox{\form@scale}{\usebox{\form@box}}%
      }{%
        \usebox{\form@box}%
      }%
    \end{minipage}%
  \ignorespacesafterend
}


%% imagesAlt is equivalent to images, without the enumeration environment. It is used for the direct placement of an image.
\newenvironment{imagesAlt}[1][]{%
  \formsetup{#1}%
  \let\questionpictureAlt\form@start@question@picture
  \ignorespaces
}{%
  \endminipage
  \endgroup
  %
  \settototalheight{\form@total@height}{\usebox{\form@box}}%
  \pgfmathsetmacro{\form@x@ratio}{\the\mywidth / \wd\form@box}%
  \pgfmathsetmacro{\form@y@ratio}{\the\myheight / \form@total@height}%
  \pgfmathsetmacro{\form@scale}{min(\form@x@ratio,
                                         \form@y@ratio)}%
  %
  \setlength{\fboxrule}{0pt}
  \fcolorbox{red!20}{blue!20}{%
    \begin{minipage}[c][\myheight][c]{\mywidth}
      \centering
      \form@ifautoscale@enabled{%
        \scalebox{\form@scale}{\usebox{\form@box}}%
      }{%
        \usebox{\form@box}%
      }%
    \end{minipage}%
  }%
  \setlength{\fboxrule}{3pt}
  \ignorespacesafterend
}

% Autoscaling technique that doesn't affect font sizes in TikZ pictures.
% (based on <https://tex.stackexchange.com/a/497749/73317>)
%
% #1: unique per-picture id allowing several pictures to use this mechanism
%     in a given document (it should contain no control sequence token nor
%     active character)
% #2: width of the reference rectangle
% #3: height of the reference rectangle
\newcommand*\form@ExportBB[3]{%
 \path let
   \p1=($(current bounding box.north east)-(current bounding box.south west)$),
   \n1={#2/\x1},\n2={#3/\y1}
 in \pgfextra{\pgfmathsetmacro{\form@figscale}{min(\n1,\n2)}%
              \expandafter\xdef\csname form@auto@figscale@#1\endcsname{%
                \form@figscale}};
 \immediate\write\@mainaux{%
   \string\expandafter
   \gdef\string\csname\space form@auto@figscale@#1\string\endcsname{%
     \csname form@auto@figscale@#1\endcsname}}}

\tikzset{scale to max size/.style args={id #1 width #2height #3}{%
    execute at end picture={\form@ExportBB{#1}{#2}{#3}},
    /utils/exec={\form@check@unique@id{#1}%
                 \ifcsname form@auto@figscale@#1\endcsname
                   \wlog{Found autoscale value for picture '#1'}%
                 \else
                   \typeout{Automatically-scaled pictures: please recompile
                            for picture '#1'.}
                   \expandafter\gdef
                     \csname form@auto@figscale@#1\endcsname{1}
                 \fi},
   scale=\csname form@auto@figscale@#1\endcsname},
         form autoscale/.style={%
           scale to max size=id #1 width \mywidth height \myheight},
         % Same style except the id is automatically generated using a counter
         form autoscale autoid/.style={%
           /utils/exec={\form@form@autscale@autoid}}}
% End of the code based on <https://tex.stackexchange.com/a/497749/73317>

\makeatother




\usepackage[most]{tcolorbox}

\newtcbox{\textbox}[1][]{
    on line,
    box align = center,
    enhanced,
    colback = gray!20,
    colframe = black,
    coltitle = black,
    boxed title style = {size = small, colback = gray!40},
    attach boxed title to top center = {yshift = -\tcboxedtitleheight/2},
    tcbox width = auto limited,
    #1,
    before = \vspace{\baselineskip} \center,
    after = \endcenter \vspace{\baselineskip}
}





\begin{document}

This Tikzpicture clearly doesn't start all the way to the left
\begin{imagesAlt}
    \begin{questionpictureAlt}
      \begin{tikzpicture}
        \draw[thick] (10,10)--(20,15);
        \draw[thick] (10,10)--(15,0);
        \draw[thick] (10,10)--(0,15);
        \draw[thick] (10,10)--(5,0);
        \draw[thick] (10,10)--(20,5);
        \draw[thick] (10,10)--(5,20);
        \draw (10.5,12) node {$1$};
        \draw (7.5,12.5) node {$2$};
        \draw (8,9.5) node {$3$};
        \draw (10,7.5) node {$4$};
        \draw (12.5,7.5) node {$5$};
        \draw (12.5,10) node {$6$};
      \end{tikzpicture}
    \end{questionpictureAlt}
  \end{imagesAlt}

Even after adding the \emph{center} environment, the picture is still not quite centered on the page
\begin{center}
  \begin{imagesAlt}
    \begin{questionpictureAlt}
      \begin{tikzpicture}
        \draw[thick] (10,10)--(20,15);
        \draw[thick] (10,10)--(15,0);
        \draw[thick] (10,10)--(0,15);
        \draw[thick] (10,10)--(5,0);
        \draw[thick] (10,10)--(20,5);
        \draw[thick] (10,10)--(5,20);
        \draw (10.5,12) node {$1$};
        \draw (7.5,12.5) node {$2$};
        \draw (8,9.5) node {$3$};
        \draw (10,7.5) node {$4$};
        \draw (12.5,7.5) node {$5$};
        \draw (12.5,10) node {$6$};
      \end{tikzpicture}
    \end{questionpictureAlt}
  \end{imagesAlt}
\end{center}

When placed inside a \emph{tcolorbox}, the picture is not centered within it, but the \emph{tcolorbox} is centered on the page.
\textbox{
  \begin{imagesAlt}
    \begin{questionpictureAlt}
      \begin{tikzpicture}
        \draw[thick] (10,10)--(20,15);
        \draw[thick] (10,10)--(15,0);
        \draw[thick] (10,10)--(0,15);
        \draw[thick] (10,10)--(5,0);
        \draw[thick] (10,10)--(20,5);
        \draw[thick] (10,10)--(5,20);
        \draw (10.5,12) node {$1$};
        \draw (7.5,12.5) node {$2$};
        \draw (8,9.5) node {$3$};
        \draw (10,7.5) node {$4$};
        \draw (12.5,7.5) node {$5$};
        \draw (12.5,10) node {$6$};
      \end{tikzpicture}
    \end{questionpictureAlt}
  \end{imagesAlt}
  }


\end{document}

特に、紫色の線は、画像が 内で中央に配置されていないtcolorboxが、tcolorbox自体は中央に配置されていることを示しています (緑色の線)。

ありがとう!

答え1

<---下記のコードでマークした箇所を確認してください。いくつかの不要なスペースが導入さ\fboxruleれ、0pt 異なる値を持つ計算 ( での計算\AfterEndEnvironment{questionpictureAlt}{...}) に使用されていました。「パッケージ」の名前を だけに変更することには反対です。formこれは、現在または将来的に他のパッケージと衝突する可能性が非常に高いことは明らかだからです (私が提供したコードでは、このような衝突からあなたを守るために、 を「パッケージ」と呼んでいますryanjform)。また、あなたが行った の 1 つが...\letを破壊しました。 これが、 で面倒な( がないことに注意してください)\form@start@question@pictureことを行わなければならなかった理由です。\let\questionpictureAlt\form@start@question@pictureAlt\newenvironment{imagesAlt}{...}{...}

もう 1 つのポイントは、が引数で指定された幅を占有しない唯一の状況であるため、をimagesAlt空の で使用するのは非常に不自然だと思うことです。私の環境での幅は、と picture-in- を加えたものがちょうど を占有するように計算されたため、これを の幅をゼロにする特殊なケースをトリガーする空の で使用することは、が基づいている環境の想定される使用法に反します。 が必要ない場合、なぜ幅の計算を行うのでしょうか。 がない場合、計算は役に立ちません。このような場合は、 を環境定義から削除し、未使用の幅の計算を削除して を入れて、これで終わりにします。minipageminipageminipagechoicesminipage\fcolorbox\linewidthminipagechoicesimagesAltminipageminipageminipage\fcolorbox

同様に、と次のimagesAltの間に印刷可能な文字を追加するとすぐに、 は設計上行幅全体を占めるため、を中央揃えにするために環境内で折り返します。\end{questionpictureAlt}\end{imagesAlt}imagesAltcenter写真通常は右余白にぴったりと収まるはずの は、非常に奇妙な手法です。imagesAltだけが含まれていれば\fcolorbox、私の控えめな意見では、はるかに自然になります。

否定的な言い方で申し訳ありませんが、これをすべて読み返すと長くて、もう遅くなってきましたし、私のchoices環境から派生したコードのこれらの奇妙な使い方は、いわば私を動揺させました (英語で適切な言葉が見つかりません。あなたは私を「殺しました」! :-) 以下のコードは、この回答の最初の段落で言及されている問題と、あなたのdocument環境の問題 (左余白から開始、中央揃えなど) を修正します。

\documentclass{article}
\usepackage{graphicx}
\usepackage{calc} % for \settototalheight (used for convenience)
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{tikz}
\usepackage{parskip}
\usepackage{enumitem}
\usetikzlibrary{calc}

% Frame dimensions
\newlength{\mywidth}
\newlength{\myheight}
\setlength{\mywidth}{3.5cm}
\setlength{\myheight}{3.5cm}

\makeatletter

% l3keys supports a key/value-style interface for the optional argument of the 'images' environment.
\ExplSyntaxOn

\msg_new:nnn { form } { duplicate-figure-id }
  { duplicate~figure~identifier:~'\exp_not:n {#1}'. }

% Whether to perform the \scalebox-based autoscaling for a given figure
\bool_new:N \l__form_do_autoscale_pic_in_images_bool
% Sequence recording all figure identifiers (for the 'scale to max size' TikZ style) found so far
\seq_new:N \g__form_scale_to_max_style_figure_ids_seq
% Counter used when generating automatic figure identifiers for 'form autoscale'
\int_new:N \g_form_last_autogenerated_figure_nb_int

% Define the options supported in the optional argument of the 'images' environment
\keys_define:nn { form }
  {
    autoscale .bool_set:N = \l__form_do_autoscale_pic_in_images_bool,
    % Value used when the 'autoscale' key is passed with no value
    autoscale .default:n = { true },
    autoscale .initial:n = { false }
  }

\cs_new_protected:Npn \__form_set_keys:n #1
  { \keys_set:nn { form } {#1} }

\cs_new_protected:Npn \__form_check_unique_id:n #1
  {
    \seq_if_in:NnTF \g__form_scale_to_max_style_figure_ids_seq {#1}
      { \msg_error:nnn { form } { duplicate-figure-id } {#1} }
      { \seq_gput_right:Nn \g__form_scale_to_max_style_figure_ids_seq {#1} }
  }

\cs_new_protected:Npn \__form_form_autoscale:n #1
  {
    \pgfkeys { /tikz/.cd, form~autoscale={#1} }
  }

\cs_generate_variant:Nn \__form_form_autoscale:n { V }

% Automatic construction of generated ids (the pattern is defined here)
\cs_new:Npn \__form_autogenerated_id:n #1
  { form~autogenerated~id~#1 }

\cs_generate_variant:Nn \__form_autogenerated_id:n { V }

\cs_new_protected:Npn \__form_form_autoscale_autoid:
  {
    % Increment the counter
    \int_gincr:N \g_form_last_autogenerated_figure_nb_int
    \tl_set:Nx \l_tmpa_tl       % generate an ID based on the counter value
      {
        \__form_autogenerated_id:V
          \g_form_last_autogenerated_figure_nb_int
      }
    % Call the 'form autoscale' style with the new ID
    \__form_form_autoscale:V \l_tmpa_tl
  }

% Set up aliases using LaTeX2e naming style
\cs_set_eq:NN \formsetup \__form_set_keys:n
\cs_set_eq:NN \form@check@unique@id \__form_check_unique_id:n
\cs_set_eq:NN \form@form@autoscale@autoid \__form_form_autoscale_autoid:

% If-then-else command using the boolean
% \l__form_do_autoscale_pic_in_images_bool to choose the branch
\NewDocumentCommand \form@ifautoscale@enabled { }
  {
    \bool_if:NTF \l__form_do_autoscale_pic_in_images_bool
  }

\ExplSyntaxOff

\newsavebox{\form@box}     % will be set with \global
\newlength{\form@total@height}

\newenvironment{questionpicture}{%
  % 'form autoscale autoid' should be turned on for every picture framed by
  % the 'images' environment
  \tikzset{every picture/.style=form autoscale autoid}%
  \begin{lrbox}{0}
}{%
  \end{lrbox}%
  \global\setbox\form@box=\box0
}

\AfterEndEnvironment{questionpicture}{%
  \par\noindent
  \setlength{\fboxsep}{0pt}%
  \begingroup
  \minipage{\dimexpr\linewidth-\mywidth-2\fboxrule-2\fboxsep}
  \begingroup
  \enumerate[label=(\Alph*), ref=\Alph*, align=left, itemsep=7pt]
}



%% questionpictureAlt is equivalent to questionpicture, without the enumerate environment. It is used for direct placement of an image.

\newenvironment{questionpictureAlt}{%
  \tikzset{every picture/.style=form autoscale autoid}%
  \begin{lrbox}{0}
}{%
  \end{lrbox}%
  \global\setbox\form@box=\box0
}

\AfterEndEnvironment{questionpictureAlt}{%
  \par\noindent
  \setlength{\fboxsep}{5pt}%
  \setlength{\fboxrule}{0pt}% <--- moved here and added % to remove space
  \begingroup
  \minipage{\dimexpr\linewidth-\mywidth-2\fboxrule-2\fboxsep}
}

\let\form@start@question@picture\questionpicture
\newcommand*{\form@invalid@place@for@calling@questionpicture}{%
  \errmessage{The 'questionpicture' environment must be used at the start of a
    'images' environment}%
}
% Generate an error message unless \questionpicture is used where expected
\let\questionpicture\form@invalid@place@for@calling@questionpicture

\let\form@start@question@pictureAlt\questionpictureAlt % <--- added Alt
\newcommand*{\form@invalid@place@for@calling@questionpictureAlt}{%
  \errmessage{The 'questionpictureAlt' environment must be used at the start of a
    'images' environment}%
}
% Generate an error message unless \questionpictureAlt is used where expected
\let\questionpictureAlt\form@invalid@place@for@calling@questionpictureAlt


\newenvironment{images}[1][]{%
  \formsetup{#1}%
  \let\questionpicture\form@start@question@picture
  \ignorespaces
}{%
  \endenumerate
  \endgroup
  \endminipage
  \endgroup
  %
  \settototalheight{\form@total@height}{\usebox{\form@box}}%
  \pgfmathsetmacro{\form@x@ratio}{\the\mywidth / \wd\form@box}%
  \pgfmathsetmacro{\form@y@ratio}{\the\myheight / \form@total@height}%
  \pgfmathsetmacro{\form@scale}{min(\form@x@ratio, \form@y@ratio)}%
  % <--- You suppressed the \fcolorbox here! Less space is occupied.
    \begin{minipage}[c][\myheight][c]{\mywidth}
      \centering
      \form@ifautoscale@enabled{%
        \scalebox{\form@scale}{\usebox{\form@box}}%
      }{%
        \usebox{\form@box}%
      }%
    \end{minipage}%
  \ignorespacesafterend
}


%% imagesAlt is equivalent to images, without the enumeration environment. It is used for the direct placement of an image.
\newenvironment{imagesAlt}[1][]{%
  \formsetup{#1}%
  \let\questionpictureAlt\form@start@question@pictureAlt % <--- added Alt
  \ignorespaces
}{%
  \endminipage
  \endgroup
  %
  \settototalheight{\form@total@height}{\usebox{\form@box}}%
  \pgfmathsetmacro{\form@x@ratio}{\the\mywidth / \wd\form@box}%
  \pgfmathsetmacro{\form@y@ratio}{\the\myheight / \form@total@height}%
  \pgfmathsetmacro{\form@scale}{min(\form@x@ratio, \form@y@ratio)}%
  %
  \fcolorbox{red!20}{blue!20}{%
    \begin{minipage}[c][\myheight][c]{\mywidth}
      \centering
      \form@ifautoscale@enabled{%
        \scalebox{\form@scale}{\usebox{\form@box}}%
      }{%
        \usebox{\form@box}%
      }%
    \end{minipage}%
  }%
  %\setlength{\fboxrule}{3pt}% <--- What was the purpose of this here?
  \ignorespacesafterend
}

% Autoscaling technique that doesn't affect font sizes in TikZ pictures.
% (based on <https://tex.stackexchange.com/a/497749/73317>)
%
% #1: unique per-picture id allowing several pictures to use this mechanism
%     in a given document (it should contain no control sequence token nor
%     active character)
% #2: width of the reference rectangle
% #3: height of the reference rectangle
\newcommand*\form@ExportBB[3]{%
 \path let
   \p1=($(current bounding box.north east)-(current bounding box.south west)$),
   \n1={#2/\x1},\n2={#3/\y1}
 in \pgfextra{\pgfmathsetmacro{\form@figscale}{min(\n1,\n2)}%
              \expandafter\xdef\csname form@auto@figscale@#1\endcsname{%
                \form@figscale}};
 \immediate\write\@mainaux{%
   \string\expandafter
   \gdef\string\csname\space form@auto@figscale@#1\string\endcsname{%
     \csname form@auto@figscale@#1\endcsname}}}

\tikzset{scale to max size/.style args={id #1 width #2height #3}{%
    execute at end picture={\form@ExportBB{#1}{#2}{#3}},
    /utils/exec={\form@check@unique@id{#1}%
                 \ifcsname form@auto@figscale@#1\endcsname
                   \wlog{Found autoscale value for picture '#1'}%
                 \else
                   \typeout{Automatically-scaled pictures: please recompile
                            for picture '#1'.}
                   \expandafter\gdef
                     \csname form@auto@figscale@#1\endcsname{1}
                 \fi},
   scale=\csname form@auto@figscale@#1\endcsname},
         form autoscale/.style={%
           scale to max size=id #1 width \mywidth height \myheight},
         % Same style except the id is automatically generated using a counter
         form autoscale autoid/.style={%
           /utils/exec={\form@form@autoscale@autoid}}}
% End of the code based on <https://tex.stackexchange.com/a/497749/73317>

\makeatother




\usepackage[most]{tcolorbox}

\newtcbox{\textbox}[1][]{
    on line,
    box align = center,
    enhanced,
    colback = gray!20,
    colframe = black,
    coltitle = black,
    boxed title style = {size = small, colback = gray!40},
    attach boxed title to top center = {yshift = -\tcboxedtitleheight/2},
    tcbox width = auto limited,
    #1,
    before = \vspace{\baselineskip}\center,
    after = \endcenter \vspace{\baselineskip}
}

\begin{document}

This Tikzpicture clearly starts all the way to the left:
\begin{imagesAlt}
    \begin{questionpictureAlt}
      \begin{tikzpicture}
        \draw[thick] (10,10)--(20,15);
        \draw[thick] (10,10)--(15,0);
        \draw[thick] (10,10)--(0,15);
        \draw[thick] (10,10)--(5,0);
        \draw[thick] (10,10)--(20,5);
        \draw[thick] (10,10)--(5,20);
        \draw (10.5,12) node {$1$};
        \draw (7.5,12.5) node {$2$};
        \draw (8,9.5) node {$3$};
        \draw (10,7.5) node {$4$};
        \draw (12.5,7.5) node {$5$};
        \draw (12.5,10) node {$6$};
      \end{tikzpicture}
    \end{questionpictureAlt}
  \end{imagesAlt}

After adding the \emph{center} environment, the picture is perfectly centered
on the page:\\*
\rule{\dimexpr 0.5\linewidth-0.5\mywidth-5pt\relax}{0.4pt}\hfill
\rule{\dimexpr 0.5\linewidth-0.5\mywidth-5pt\relax}{0.4pt}\vspace{-1.6ex}%
\begin{center} % <--- It doesn't make sense to use imagesAlt inside center
  \begin{imagesAlt}
    \begin{questionpictureAlt}
      \begin{tikzpicture}
        \draw[thick] (10,10)--(20,15);
        \draw[thick] (10,10)--(15,0);
        \draw[thick] (10,10)--(0,15);
        \draw[thick] (10,10)--(5,0);
        \draw[thick] (10,10)--(20,5);
        \draw[thick] (10,10)--(5,20);
        \draw (10.5,12) node {$1$};
        \draw (7.5,12.5) node {$2$};
        \draw (8,9.5) node {$3$};
        \draw (10,7.5) node {$4$};
        \draw (12.5,7.5) node {$5$};
        \draw (12.5,10) node {$6$};
      \end{tikzpicture}
    \end{questionpictureAlt}
  \end{imagesAlt}
\end{center}

When placed inside a \emph{tcolorbox}, the picture is centered within it, and
the \emph{tcolorbox} is centered on the page.
\textbox{%
  \begin{imagesAlt}
    \begin{questionpictureAlt}
      \begin{tikzpicture}
        \draw[thick] (10,10)--(20,15);
        \draw[thick] (10,10)--(15,0);
        \draw[thick] (10,10)--(0,15);
        \draw[thick] (10,10)--(5,0);
        \draw[thick] (10,10)--(20,5);
        \draw[thick] (10,10)--(5,20);
        \draw (10.5,12) node {$1$};
        \draw (7.5,12.5) node {$2$};
        \draw (8,9.5) node {$3$};
        \draw (10,7.5) node {$4$};
        \draw (12.5,7.5) node {$5$};
        \draw (12.5,10) node {$6$};
      \end{tikzpicture}
    \end{questionpictureAlt}
  \end{imagesAlt}%
}

\end{document}

スクリーンショット

関連情報