将不同的 tikzpicture 的两个节点对齐到页面的中心

将不同的 tikzpicture 的两个节点对齐到页面的中心

我对两个 tikzpicture 环境的对齐存在问题。

\documentclass[14pt,spanish]{extarticle}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Ubuntu}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}
\usepackage{subscript}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{textgreek}
\usepackage{mhchem}

\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{inicio} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=yellow]
\tikzstyle{nodo} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black]
\tikzstyle{arrow} = [thick,->,>=stealth]

\makeatother

\usepackage{babel}
\addto\shorthandsspanish{\spanishdeactivate{~<>}}

\usepackage{xunicode}
\begin{document}

\begin{figure}[h!]
    \centering
    \begin{tikzpicture}[node distance=2cm]  
        \node (fosforo) [inicio] {Fósforo};

        \node (ortofosfato) [nodo, below of=fosforo] {Ortofosfato (\ce{P-PO_4^{3-}})}; \draw [arrow] (fosforo) -- node [anchor=west] {medido como} (ortofosfato);
    \end{tikzpicture}
\end{figure}

\begin{figure}[h!]
    \centering
    \begin{tikzpicture}[node distance=2cm]
        \node (nitrogeno) [inicio] {Nitrógeno};

        \node (nitrato) [nodo, below of=nitrogeno, xshift=-2.5cm] {Nitrato (\ce{N-NO_3^{-}})}; \draw [arrow] (nitrogeno) -- node [anchor=east,xshift=-0.5cm] {medido como} (nitrato);
        \node (amonio) [nodo, below of=nitrogeno, xshift=2.5cm] {Amonio (\ce{N-NH_4^+})}; \draw [arrow] (nitrogeno) -- node [anchor=west,xshift=0.5cm] {medido como} (amonio);
    \end{tikzpicture}
\end{figure}

\end{document}

我希望节点“fosforo”和“nitrogeno”在页面中心水平对齐。我该怎么做?

答案1

您的顶部节点位于 的中心(0,0),因此您可以使用trim lefttrim right使图片关于此轴对称。由于同一行上没有其他内容,因此足以使图片宽度为零。因此,您可以添加

trim left=0pt,trim right=0pt

作为这些图片的选项。下面我centerzero为此定义了一种样式:

示例输出

\documentclass[14pt,spanish]{extarticle}
\usepackage{fontspec}
%\setmainfont[Mapping=tex-text]{Ubuntu}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}
\usepackage{subscript}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{textgreek}
\usepackage{mhchem}

\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{inicio} = [rectangle, rounded corners, minimum width=3cm,
  minimum height=1cm,text centered, draw=black, fill=yellow] 
\tikzstyle{nodo} = [rectangle, rounded corners, minimum width=3cm,
  minimum height=1cm,text centered, draw=black] 
\tikzstyle{arrow} = [thick,->,>=stealth]
\tikzstyle{centerzero} = [trim left=0pt,trim right=0pt]

\usepackage{babel}
\addto\shorthandsspanish{\spanishdeactivate{~<>}}

\usepackage{xunicode}
\begin{document}

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}[node distance=2cm,centerzero]  
        \node (fosforo) [inicio] {Fósforo};

        \node (ortofosfato) [nodo, below of=fosforo] {Ortofosfato
          (\ce{P-PO_4^{3-}})}; 
        \draw [arrow] (fosforo) -- node [anchor=west] {medido como}
          (ortofosfato); 
    \end{tikzpicture}
\end{figure}

\begin{figure}[htp]
    \centering
    \begin{tikzpicture}[node distance=2cm,centerzero]
        \node (nitrogeno) [inicio] {Nitrógeno};

        \node (nitrato) [nodo, below of=nitrogeno, xshift=-2.5cm]
          {Nitrato (\ce{N-NO_3^{-}})}; 
        \draw [arrow] (nitrogeno) -- node [anchor=east,xshift=-0.5cm]
          {medido como} (nitrato); 
        \node (amonio) [nodo, below of=nitrogeno, xshift=2.5cm]
          {Amonio (\ce{N-NH_4^+})}; 
        \draw [arrow] (nitrogeno) -- node [anchor=west,xshift=0.5cm]
          {medido como} (amonio); 
    \end{tikzpicture}
\end{figure}

\end{document}

或者,如果你想确保这个图形有一个固定的非零宽度,你可以把

trim left=-8cm,trim right=8cm

使中间水平方向tikzpicture宽16厘米。(0,0)

顺便说一句,您制作的这些figures 带有放置选项h!,但没有标题。如果您只想要当前位置的图片(没有标题),那么环境center更合适。

答案2

我将两个 tikzpicture 放在一个图形环境中。这样它们就并排放置了。我在图形前后添加了垂直空间,使其垂直居中。

\documentclass[14pt,spanish]{extarticle}
\usepackage{fontspec}
\setmainfont[Mapping=tex-text]{Ubuntu}
\usepackage{geometry}
\geometry{verbose,tmargin=2cm,bmargin=2cm,lmargin=2cm,rmargin=2cm}
\usepackage{subscript}

\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{textgreek}
\usepackage{mhchem}

\usetikzlibrary{shapes.geometric, arrows}

\tikzstyle{inicio} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=yellow]
\tikzstyle{nodo} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black]
\tikzstyle{arrow} = [thick,->,>=stealth]

\makeatother

\usepackage{babel}
\addto\shorthandsspanish{\spanishdeactivate{~<>}}

\usepackage{xunicode}
\begin{document}

\vspace*{\fill}
\begin{figure}[h!]
    \centering
    \begin{tikzpicture}[node distance=2cm]  
        \node (fosforo) [inicio] {Fósforo};

        \node (ortofosfato) [nodo, below of=fosforo] {Ortofosfato (\ce{P-PO_4^{3-}})}; \draw [arrow] (fosforo) -- node [anchor=west] {medido como} (ortofosfato);
    \end{tikzpicture}
%\end{figure}%
%\begin{figure}[h!]
%    \centering
    \begin{tikzpicture}[node distance=2cm]
        \node (nitrogeno) [inicio] {Nitrógeno};

        \node (nitrato) [nodo, below of=nitrogeno, xshift=-2.5cm] {Nitrato (\ce{N-NO_3^{-}})}; \draw [arrow] (nitrogeno) -- node [anchor=east,xshift=-0.5cm] {medido como} (nitrato);
        \node (amonio) [nodo, below of=nitrogeno, xshift=2.5cm] {Amonio (\ce{N-NH_4^+})}; \draw [arrow] (nitrogeno) -- node [anchor=west,xshift=0.5cm] {medido como} (amonio);
    \end{tikzpicture}
\end{figure}
\vfill

\end{document}

相关内容