Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a problem with the alignment of two tikzpicture environments.

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

I want that the nodes "fosforo" and "nitrogeno" get aligned horizontally in the center of the page. How can I do that?

share|improve this question
    
Hi, welcome to TeX.SX. Could you please provide a Minimum (non-)Working Example so that those who want to help can generate your problem. As it stands, this does not compile. –  Jesse Jun 23 at 2:45
    
@Jesse Should I put all the code generated by Lyx? –  Mario Lamas Espinoza Jun 23 at 2:55
    
Not all, just make it to minimal that shows your problem. –  Jesse Jun 23 at 2:58
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. –  Sean Allred Jun 23 at 3:09

2 Answers 2

up vote 2 down vote accepted

Your top node is centered on (0,0), so you can use trim left and trim right to make the picture symmetric about this axis. As there is nothing else on the same line, it is enough to make the picture of zero width. Thus you can add

trim left=0pt,trim right=0pt

as the options to these pictures. In the following I have defined a style centerzero for this:

Sample output

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

Alternatively, if you wanted to make sure this figures have a fixed non-zero width, you could put

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

making the tikzpicture 16cm wide with (0,0) in the middle horizontally.

Incidentally you have made these figures with placement options h! and no caption. If you just want a picture at the current position (without caption) then the center environment is much more appropriate.

share|improve this answer

I put the two tikzpictures inside one figure environment. That puts them side by side. I added vertical spaces before and after the figure to have it centered vertically.

\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}
share|improve this answer
    
The solution is good for this two tikzpicture, but I have other tikzpicture in my document in other pages. I want that all the principal nodes are aligned in the center of the page. –  Mario Lamas Espinoza Jun 23 at 3:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.