# Left alignment in TikZ

I'm not able to align the text left.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand{\altura}{.45cm}
\begin{document}

\begin{tikzpicture}[x=1cm, y=-\altura, node distance=0,outer sep=0,inner sep=0]
\tikzstyle{nome}=[draw, rectangle,anchor=west, minimum height=\altura,minimum width=9cm,fill=yellow!30]

\node[nome,right] (p1) {text};
\node[nome,right] (p2) [below = of p1] {text text};
\node[nome,right] (p3) [below  = of p2] {text text text};

\end{tikzpicture}
\end{document}


The left or anchor=west not work. I need the text align left.

-

You could add an option specifying the text width. It's automatically left justified then:

\tikzstyle{nome}=[draw, rectangle,anchor=west, minimum height=\altura,
minimum width=9cm,fill=yellow!30,text width=8.8cm]


-
text width, off corse. Thank you Stefan. –  Regis da Silva Mar 9 '11 at 23:53

I just want to add a word of explanation as to why the observed behaviour is happening and so why the already given solutions (from Stefan and Gonzalo) are the right things to do.

The part to focus on is what happens when you pass the option minimum size (or height or width) to Tikz. What is does is as follows (or at least, the following is equivalent to what it does): it first constructs the node as specified but without the minimum size. It then measures the relevant size of the node and compares it to the specified minimum. If the node is smaller, then it enlarges it to the minimum size. The crucial thing to realise is that it does this by increasing the padding. That is, it increases the border between the contents of the node and the shape. So the contents of the node (the text) do not see the increase in size. As far as the text is concerned, it is still in the same box that is was before (which was just big enough to contain it). As the padding is increased uniformly wherever it is needed, the net effect is to keep the text in the centre of the node. Thus to force left-alignment, the text needs to be told that it can have more space, which is what the two given solutions do.

To see that this is so, we can try padding the text with \hfills to see how big the text boxes are (alternating the original with Stefan's solution):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand{\altura}{.45cm}
\begin{document}

\begin{tikzpicture}[x=1cm, y=-\altura, node distance=0,outer sep=0,inner sep=0]
\tikzstyle{nome}=[draw, rectangle,anchor=west, minimum height=\altura,minimum width=9cm,fill=yellow!30]
\tikzstyle{widenome}=[draw, rectangle,anchor=west, minimum height=\altura,text width=9cm,fill=yellow!30]

\node[nome,right] (p1) {\rule{1ex}{1ex}\hfill text\hfill\rule{1ex}{1ex}};
\node[widenome,right] (p2) [below = of p1] {\rule{1ex}{1ex}\hfill text\hfill\rule{1ex}{1ex}};
\node[nome,right] (p3) [below = of p2] {\rule{1ex}{1ex}\hfill text text\hfill\rule{1ex}{1ex}};
\node[widenome,right] (p4) [below = of p3] {\rule{1ex}{1ex}\hfill text text\hfill\rule{1ex}{1ex}};
\node[nome,right] (p5) [below  = of p4] {\rule{1ex}{1ex}\hfill text text text\hfill\rule{1ex}{1ex}};
\node[widenome,right] (p6) [below  = of p5] {\rule{1ex}{1ex}\hfill text text text\hfill\rule{1ex}{1ex}};

\end{tikzpicture}
\end{document}


Result:

So we can see that in the original specification, the text thought that it was in a box just big enough to hold itself. So it was left aligned, as far as it could tell!

-

One option would be to use boxes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{patterns}

\begin{document}

\newcommand{\altura}{.45cm}
\begin{tikzpicture}[x=1cm, y=-\altura, node distance=0,outer sep=0,inner sep=0]
\tikzstyle{nome}=[draw, rectangle, minimum height=\altura,fill=yellow!30,minimum width=9cm]

\node[nome] (p1) {\parbox{9cm}{text}};
\node[nome] (p2) [below = of p1] {\parbox{9cm}{text text}};
\node[nome] (p3) [below  = of p2] {\parbox{9cm}{text text text}};

\end{tikzpicture}

\end{document}

-
I was just going to post the same solution just with \makebox{9cm}[l] instead of \parbox. –  Martin Scharrer Mar 8 '11 at 17:40