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'm trying to align horizontally three grids, two of them in one figure. Below is the sample code:

\documentclass[twocolumn]{article}
%% -------------------------------------------------------------------------------------
\usepackage{tikz,pgfplots}

\makeatletter
\def\grd@save@target#1{%
  \def\grd@target{#1}}
\def\grd@save@start#1{%
  \def\grd@start{#1}}
\tikzset{
  grid with coordinates/.style={
    to path={%
      \pgfextra{%
        \edef\grd@@target{(\tikztotarget)}%
        \tikz@scan@one@point\grd@save@target\grd@@target\relax
        \edef\grd@@start{(\tikztostart)}%
        \tikz@scan@one@point\grd@save@start\grd@@start\relax
        \draw[minor help lines] (\tikztostart) grid (\tikztotarget);
        \draw[major help lines] (\tikztostart) grid (\tikztotarget);
        \grd@start
        \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm}
        \grd@target
        \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm}
        \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm}
        \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step x}}
        \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}}
        \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb}
        \node[anchor=north] at (\x,\grd@ya -0.05) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\x}};       
        \foreach \y in {\grd@ya,\grd@yc,...,\grd@yb}
        \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\y}};
      }
    }
  },
  minor help lines/.style={
    help lines,
    black!20!white,
    line cap =round,
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y}
  },
  major help lines/.style={
    help lines,
    black!20!white,
    line cap =round,
    line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width},
    xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x},
    ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y}
  },
  grid with coordinates/.cd,
  minor step x/.initial=.5,
  minor step y/.initial=.2,
  major step x/.initial=1.0,
  major step y/.initial=1.0,
  major line width/.initial=1pt,
  grid precision x/.initial=1,
}
\makeatother
%% -------------------------------------------------------------------------------------

\begin{document}

\begin{figure}[!b]
    \centering
    \begin{tikzpicture}[xscale=0.1,yscale=1,font=\scriptsize,grid with coordinates/major step x=10,grid with coordinates/minor step x=2]
        \draw (0,0) to[grid with coordinates] (50,1);

        \coordinate [label=above:``low''] (A) at (5,1.0);
        \draw[thick,line cap =round] (0,1) -- (10,1) -- (20,0);
        \coordinate [label=above:``average''] (A) at (20,1.0);
        \draw[thick,line cap =round] (10,0) -- (20,1) -- (30,0);        
        \coordinate [label=above:``high''] (A) at (35,1.0);
        \draw[thick,line cap =round] (20,0) -- (30,1) -- (50,1);

    \end{tikzpicture}
    \begin{tikzpicture}[xscale=1,yscale=1,font=\scriptsize,grid with coordinates/minor step x=0.2]
        \draw (0,0) to[grid with coordinates] (5,1);

        \coordinate [label=above:``low''] (A) at (0.5,1.0);
        \draw[thick,line cap =round] (0,1) -- (1,1) -- (2,0);
        \coordinate [label=above:``average''] (A) at (2,1.0);
        \draw[thick,line cap =round] (1,0) -- (2,1) -- (3,0);       
        \coordinate [label=above:``high''] (A) at (3.5,1.0);
        \draw[thick,line cap =round] (2,0) -- (3,1) -- (5,1);

    \end{tikzpicture}
    \caption{Membership functions of 12-month ROR ($C$ -- upper) and 1-month ROR ($P$ -- lower).}\label{fig:predicatesRORmemF}
\end{figure}
\begin{figure}[!b]
    \centering
    \begin{tikzpicture}[xscale=5,yscale=1,font=\scriptsize,,grid with coordinates/major step x=0.2,grid with coordinates/minor step x=0.04]
        \draw (0,0) to[grid with coordinates] (1,1);

        \coordinate [label=above:${Q}_1$ -- ``majority''] (A) at (0.5,1.0);
        \draw[thick,dashed,line cap =round] (0.3,0) -- (0.5,1) -- (1.0,1) -- (1.0,0);
        \coordinate [label=above:${Q}_2$ -- ``most''] (A) at (0.90,1.0);
        \draw[thick,line cap =round] (0.3,0) -- (0.8,1) -- (1.0,1) -- (1.0,0);      

    \end{tikzpicture}
    \caption{${Q}_1$ (``majority'') and ${Q}_2$ (``most'') linguistic quantifiers' membership functions.}\label{fig:quantifiersMemF}
\end{figure}

\end{document}

and screen capture of resulting PDF:

Valid XHTML.

The small difference comes from "5.0" and "50.0" width, as far as I understand.

I've tried

\pgfresetboundingbox
\path (-10.1) rectangle (60.0);

and

\path (-1.1) rectangle (6.0);

from tikzpicture alignment and centering with no result.

Is there any easy solution for this problem? I only need to use it on those 3 grids/2 figures.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

You probably want ,, not . as your coordinate separator.

The following works for me: \path (-10, 0) -- (60, 0); in the first and \path (-1, 0) -- (6, 0) in the second diagram.

share|improve this answer

Just add bounding box specifications in each tikzpicture environment. I modified your code below. I added a vertical red line to indicate the alignment.

\documentclass[twocolumn]{article}
%% -------------------------------------------------------------------------------------
\usepackage{tikz,pgfplots}

\makeatletter \def\grd@save@target#1{% \def\grd@target{#1}} \def\grd@save@start#1{% \def\grd@start{#1}} \tikzset{ grid with coordinates/.style={ to path={% \pgfextra{% \edef\grd@@target{(\tikztotarget)}% \tikz@scan@one@point\grd@save@target\grd@@target\relax \edef\grd@@start{(\tikztostart)}% \tikz@scan@one@point\grd@save@start\grd@@start\relax \draw[minor help lines] (\tikztostart) grid (\tikztotarget); \draw[major help lines] (\tikztostart) grid (\tikztotarget); \grd@start \pgfmathsetmacro{\grd@xa}{\the\pgf@x/1cm} \pgfmathsetmacro{\grd@ya}{\the\pgf@y/1cm} \grd@target \pgfmathsetmacro{\grd@xb}{\the\pgf@x/1cm} \pgfmathsetmacro{\grd@yb}{\the\pgf@y/1cm} \pgfmathsetmacro{\grd@xc}{\grd@xa + \pgfkeysvalueof{/tikz/grid with coordinates/major step x}} \pgfmathsetmacro{\grd@yc}{\grd@ya + \pgfkeysvalueof{/tikz/grid with coordinates/major step y}} \foreach \x in {\grd@xa,\grd@xc,...,\grd@xb} \node[anchor=north] at (\x,\grd@ya -0.05) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\x}};
\foreach \y in {\grd@ya,\grd@yc,...,\grd@yb} \node[anchor=east] at (\grd@xa,\y) {\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{\y}}; } } }, minor help lines/.style={ help lines, black!20!white, line cap =round, xstep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step x}, ystep=\pgfkeysvalueof{/tikz/grid with coordinates/minor step y} }, major help lines/.style={ help lines, black!20!white, line cap =round, line width=\pgfkeysvalueof{/tikz/grid with coordinates/major line width}, xstep=\pgfkeysvalueof{/tikz/grid with coordinates/major step x}, ystep=\pgfkeysvalueof{/tikz/grid with coordinates/major step y} }, grid with coordinates/.cd, minor step x/.initial=.5, minor step y/.initial=.2, major step x/.initial=1.0, major step y/.initial=1.0, major line width/.initial=1pt, grid precision x/.initial=1, } \makeatother %% ------------------------------------------------------------------------------------- \pgfplotsset{compat=1.8}

\begin{document}

\begin{figure}[!h] \centering \begin{tikzpicture}[xscale=0.1,yscale=1,font=\scriptsize,grid with coordinates/major step x=10,grid with coordinates/minor step x=2] \useasboundingbox (0,-1) rectangle (50,1); \draw (0,0) to[grid with coordinates] (50,1); \coordinate [label=above:low''] (A) at (5,1.0); \draw[thick,line cap =round] (0,1) -- (10,1) -- (20,0); \coordinate [label=above:average''] (A) at (20,1.0); \draw[thick,line cap =round] (10,0) -- (20,1) -- (30,0);
\coordinate [label=above:high''] (A) at (35,1.0); \draw[thick,line cap =round] (20,0) -- (30,1) -- (50,1); \end{tikzpicture} \begin{tikzpicture}[xscale=1,yscale=1,font=\scriptsize,grid with coordinates/minor step x=0.2] \useasboundingbox (0,-0.3) rectangle (5,1); \draw (0,0) to[grid with coordinates] (5,1); \coordinate [label=above:low''] (A) at (0.5,1.0); \draw[thick,line cap =round] (0,1) -- (1,1) -- (2,0); \coordinate [label=above:average''] (A) at (2,1.0); \draw[thick,line cap =round] (1,0) -- (2,1) -- (3,0);
\coordinate [label=above:
high''] (A) at (3.5,1.0); \draw[thick,line cap =round] (2,0) -- (3,1) -- (5,1); \end{tikzpicture} \caption{Membership functions of 12-month ROR ($C$ -- upper) and 1-month ROR ($P$ -- lower).}\label{fig:predicatesRORmemF} \end{figure} \begin{figure}[!h] \centering \begin{tikzpicture}[xscale=5,yscale=1,font=\scriptsize,,grid with coordinates/major step x=0.2,grid with coordinates/minor step x=0.04] \useasboundingbox (0,-0.3) rectangle (1,1); \draw (0,0) to[grid with coordinates] (1,1); \coordinate [label=above:${Q}_1$ -- majority''] (A) at (0.5,1.0); \draw[thick,dashed,line cap =round] (0.3,0) -- (0.5,1) -- (1.0,1) -- (1.0,0); \coordinate [label=above:${Q}_2$ --most''] (A) at (0.90,1.0); \draw[thick,line cap =round] (0.3,0) -- (0.8,1) -- (1.0,1) -- (1.0,0); \end{tikzpicture} \caption{${Q}_1$ (majority'') and ${Q}_2$ (most'') linguistic quantifiers' membership functions.}\label{fig:quantifiersMemF} \end{figure} \begin{tikzpicture}[red] \useasboundingbox (0,0) rectangle (0,0); \draw (6.19,0) -- +(0,10); \end{tikzpicture} \end{document}

share|improve this answer

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.