Tikz 图形居中问题

Tikz 图形居中问题

我试图将这个特定的 Tikz 树形图置于我的论文的中心:

在此处输入图片描述

我正在使用以下代码:

\begin{center}
    \begin{figure}[H]   
        \begin{forest}                      
            for tree={grow'=0,align=left,l sep=6em,
                anchor=center,parent anchor=east,child anchor=west,
                edge={semithick,-stealth,shorten >=1em}},
            [$x$ (consumer)\\
            or
            $-x$ (producer),alias=root
            [{$u(c)$\\
                if consumer\\
            },
            edge label={node[pos=0.4,above]{$1/2$}},
            alias=c1,inner ysep=0pt]
            [{$g(y)$\\
                if producer\\
            },
            edge label={node[pos=0.4,below]{$1/2$}},
            alias=c2,inner ysep=0pt]
            ]
            \path ([yshift=-1ex]current bounding box.south) coordinate (aux);
            \begin{scope}[decoration={calligraphic brace, amplitude=6pt},line width=1pt]
                \draw[decorate] (aux-|root.east) --
                (aux-|root.west) node[midway,below=1ex]{Day};
                \draw[decorate] (aux-|c1.east) --
                ([xshift=-1ex]c1.west|-aux) node[midway,below=1ex]{Night};
                \draw[decorate] (c1.south west) -- (c1.north west);
                \draw[decorate] (c2.south west) -- (c2.north west);
            \end{scope} 
        \end{forest}
        \caption{The physical environment\label{fig:1}}
    \end{figure}
\end{center}  

关于如何更改序言以使 Tikz 树形图居中,有什么建议吗?

我的序言:

\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{xfrac}
\usepackage[retainorgcmds]{IEEEtrantools}
\usepackage{graphicx}
\usepackage{tabularx}
\usepackage{subfig}
\usepackage{float}%keeps figures in the text where the page the code is written
\usepackage{pgfplots}
\usepackage{tikz}
\tikzset{
    mybox/.style={rounded rectangle,draw=black,align=center},
}
\usetikzlibrary{backgrounds,intersections}
\usetikzlibrary{positioning,shapes.misc}
\usepackage[edges]{forest}
\usetikzlibrary{decorations.pathreplacing,calligraphy}
\usetikzlibrary{arrows.meta,
    positioning,
    quotes,
    shadows, shapes.misc}
%Fonts
%\usepackage{times}
%\usepackage{kpfonts}    % for nice fonts
%\usepackage{times}
\usepackage{setspace} 
\usepackage{microtype} 
\usepackage{booktabs}   % for nice tables
\usepackage{multirow} % tables feature
\usepackage[flushleft]{threeparttable}
\usepackage{array}%position table cell text
%\usepackage{bbold}         % for bold math
\usepackage{dsfont}
\usepackage{listings}   % for inserting code
\usepackage{verbatim}   % useful for program listings
\usepackage{color}  
\usepackage[colorlinks=true, linkcolor=blue, urlcolor=blue, citecolor=blue]{hyperref}
% use for hypertext
\usepackage[colorinlistoftodos]{todonotes}%used to add notes on PDF
%\usepackage{natbib}
%\bibpunct{(}{)}{;}{;}{,}{,}
\usepackage[colon]{natbib}%package for bibliography
\setcounter{secnumdepth}{3} %the depth of section numbering 0 means no numbering of any section, sub-section and on.
%used for begining theorems and propostions numbered according to the section they are in
\newtheorem{theorem}{Theorem}[section]
\newtheorem{corollary}{Corollary}[theorem]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{assumption}[theorem]{Assumption}
\newtheorem{definition}[theorem]{Definition}
\usepackage{pdflscape}
\usepackage{afterpage}
\usepackage{capt-of}
\setlength{\parskip}{1em}
\usepackage{amssymb} 
%% Shortcuts
\newcommand{\td}[2]{\dfrac{d #1}{d #2}}
\newcommand{\std}[2]{\dfrac{d^2 #1}{d {#2}^2}}
\newcommand{\ctd}[3]{\dfrac{d^2 #1}{d #2 d #3}}

\newcommand{\pd}[2]{\dfrac{\partial #1}{\partial #2}}
\newcommand{\spd}[2]{\dfrac{\partial^2 #1}{\partial {#2}^2}}
\newcommand{\cpd}[3]{\dfrac{\partial^2 #1}{\partial #2 \partial #3}}

\newcommand{\pdi}[2]{\partial #1/\partial #2}

\newcommand{\LR}{\Leftrightarrow}
\newcommand{\Lg}{\mathcal{L}}
\newcommand{\half}{\tfrac{1}{2}}
\newcommand{\eqp}{\phantom{=}}
\newcommand{\eqs}{\buildrel s \over =}
\newcommand{\me}{\mathrm{e}}
\makeatletter
\newcommand*\rel@kern[1]{\kern#1\dimexpr\macc@kerna}
\newcommand*\widebar[1]{%
    \begingroup
    \def\mathaccent##1##2{%
        \rel@kern{0.8}%
        \overline{\rel@kern{-0.8}\macc@nucleus\rel@kern{0.2}}%
        \rel@kern{-0.2}%
    }%
    \macc@depth\@ne
    \let\math@bgroup\@empty \let\math@egroup\macc@set@skewchar
    \mathsurround\z@ \frozen@everymath{\mathgroup\macc@group\relax}%
    \macc@set@skewchar\relax
    \let\mathaccentV\macc@nested@a
    \macc@nested@a\relax111{#1}%
    \endgroup
}
\makeatother

\begin{document}

答案1

像这样:

在此处输入图片描述

(红线表示文本边框区域)

正如我在评论中所说:有史以来较新的在固定环境中插入浮动环境(figure, )table\begin{center} ) ...\end{center}. 你应该这样写:

\begin{figure}[ht]
\begin{center}
\begin{forest}
...
\end{forest}
\end{center}
\end{figure}

然而,更好的方法是使用\centering之后的命令\begin{figure}[...],因为它不会在图像周围引入额外的垂直空间。

完整的 MWE(仅带有必要的前言)显示您的树形图,如下所示:

\documentclass[12pt,letterpaper]{article}
\usepackage[margin=1in]{geometry}
\usepackage[english]{babel}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,
                decorations.pathreplacing,calligraphy,
                positioning,
                quotes,
                shadows, shapes.misc}
\usepackage{microtype}

\begin{document}
    \begin{figure}[ht]
    \centering
        \begin{forest}
            for tree={grow'=0,align=left,l sep=6em,
                anchor=center,parent anchor=east,child anchor=west,
                edge={semithick,-stealth,shorten >=1em}},
            [$x$ (consumer)\\
            or
            $-x$ (producer),alias=root
            [{$u(c)$\\
                if consumer\\
            },
            edge label={node[pos=0.4,above]{$1/2$}},
            alias=c1,inner ysep=0pt]
            [{$g(y)$\\
                if producer\\
            },
            edge label={node[pos=0.4,below]{$1/2$}},
            alias=c2,inner ysep=0pt]
            ]
            \path ([yshift=-1ex]current bounding box.south) coordinate (aux);
            \begin{scope}[decoration={calligraphic brace, amplitude=6pt},line width=1pt]
                \draw[decorate] (aux-|root.east) --
                (aux-|root.west) node[midway,below=1ex]{Day};
                \draw[decorate] (aux-|c1.east) --
                ([xshift=-1ex]c1.west|-aux) node[midway,below=1ex]{Night};
                \draw[decorate] (c1.south west) -- (c1.north west);
                \draw[decorate] (c2.south west) -- (c2.north west);
            \end{scope}
        \end{forest}
        \caption{The physical environment\label{fig:1}}
    \end{figure} 
\end{document}

相关内容