2
\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}
\tikzstyle{every node}=[font=\footnotesize]

\node[anchor=north west, inner sep=0, rectangle, red, draw, minimum width=0.5cm](A) {%
\begin{minipage}[t]{0.5cm}%
x\\
x
\end{minipage}};

\node[inner sep=0, rectangle, blue, draw, minimum width=0.5cm, anchor=north west](B) at (0.5cm, -\baselineskip) {%
\begin{minipage}[t]{0.5cm}%
x\\
x
\end{minipage}};

\end{tikzpicture}
\end{document}

enter image description here

How can I align the two rectangles that it looks like...

enter image description here

You get this result if \tikzstyle{every node}=[font=\footnotesize] is commented out. The problem is that I need a smaller font size. I have tried to set the shift to -\footnotesep but no success.

David Carlisle
  • 757,742
user4811
  • 4,185
  • 1
    \baselineskip under \footnotesize is 9.5pt; see {\footnotesize\the\baselineskip}. – Werner Jan 08 '14 at 07:00
  • 1
    As in the previous question, the title is misleading, the issue is not with \\ but rather a tikz question of how to propagate alignment from inner boxes to the outer node. The alignment you want is automatic with \begin{minipage}[b]{5mm}...\end{minipage}\begin{minipage}[t]{5mm}...\end{minipage} so your question is not about \\ and \baselineskip but about how to add the tikz boxes without breaking alignment. (Also it should not be tagged pdftex as you see same with classic tex or xetex or luatex) – David Carlisle Jan 08 '14 at 09:50
  • @Werner: Is it possible to set a new length to '\footnotesize\the\baselineskip' so that I can use it in Tikz? I have tried '\newlength{\mylength}\setlength{\mylength}{\footnotesize\the\baselineskip}' but no success. – user4811 Jan 09 '14 at 15:44
  • 1
    @user4811: Of course, just \setlength{\mylength}{9.5pt} would work. :) But you can also try {\footnotesize\global\setlength{\mylength}{\baselineskip}}. – Werner Jan 09 '14 at 15:49
  • @Werner: Woohoo, this works! Thx! – user4811 Jan 09 '14 at 15:53

1 Answers1

3

If you can use TiKZ v 3.0.0, it introduces a new node option called node font which is used to compute all dimensions of node, while font option only affects text inside the node and not to its dimensions.

With this version is easy to get what you want:

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\verb+node font=\footnotesize+

\begin{tikzpicture}[every node/.style={node font=\footnotesize}]
    \node[anchor=north west, inner sep=0, rectangle, red, draw, 
          text width=0.5cm, align=left](A) {%
          x\\
          x
    };
    \node[inner sep=0, rectangle, blue, draw, 
          text width=0.5cm, align=left, anchor=north west](B) 
          at (0.5cm, -\baselineskip) {%
          x\\
          x
    };
\end{tikzpicture}

\verb+font=\footnotesize+

\begin{tikzpicture}[every node/.style={font=\footnotesize}]
    \node[anchor=north west, inner sep=0, rectangle, red, draw, 
          text width=0.5cm, align=left](A) {%
          x\\
          x
    };
    \node[inner sep=0, rectangle, blue, draw, 
          text width=0.5cm, align=left, anchor=north west](B) 
          at (0.5cm, -\baselineskip) {%
          x\\
          x
    };
\end{tikzpicture}
\end{document}

enter image description here

I've also made some changes to your code:

  1. used /.style instead of tikzstyle. Please look at Should \tikzset or \tikzstyle be used to define TikZ styles?
  2. deleted minipages inside nodes. Text node is similar to a minipage, you need to fix a text width and some align option to use \\ inside nodes to break lines.
Ignasi
  • 136,588
  • Thx! This solved at least the alignment problem of a node shifted by \baselineskip. Do you know how I can shift an arrow by \baselineskip? Adding "\draw[->] (0.6cm, 0) -- (2cm, 0); \draw[->] (1.1cm, -\baselineskip) -- (2cm, -\baselineskip);" to your example illustrate the problem. The \baselineskip is different for the "arrow shift" and I don't know why. – user4811 Jan 08 '14 at 16:27