3

I've got some coordinates in a -picture and I'd like to draw some rectangles defined by two opposing corner coordinates. The seemingly easiest way to do that I found with this answer: Placing a rectangular node in tikz by specifying two opposing corners. However I can't manage to get the text vertically and horizontally centered within the drawn rectangle.

I fiddled around with various anchor points within my tikzset{...} and the text centered property, but they all seem to only affect the box, not the text.


MWE:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{fit}
\tikzset{
    block/.style 2 args = {
        draw, line width = 1pt, inner sep=0pt, outer sep=0pt,
        fit=(#1) (#2),}
}


\begin{document}

    \begin{tikzpicture}[x=1mm,y=1mm]

    \draw[step=1cm,gray,very thin] (-10,-10) grid (70,70);

    \coordinate[](0_r1) at (0,22.5);
    \coordinate[](0_r2) at (0,42);
    \coordinate[](0_r3) at (0,49);

    \coordinate[](b1_r2) at (14.4,42);
    \coordinate[](b1_r3) at (14.4,49);
    \coordinate[](b1_r4) at (14.4,59,0);

    \node[block={0_r1}{b1_r2}] {$R_{mki1}$};
    \node[block={0_r2}{b1_r3}] {$R_{mr1}$};
    \node[block={0_r3}{b1_r4}] {$R_{mko1}$};

    \end{tikzpicture}

\end{document}

enter image description here

I'm open for other ways for drawing the rectangles, but they need to be defined by predefined coordinates. Also I need to vary the line styles and filling of every rectangle, so I'd like to keep the tikzset as simple as possible and define this properties outside.

0

2 Answers 2

4

This is a known issue, and it has been treated before1 but it could be a problem in the code for the fit library.2

However you can use the solution found in one of those questions and use the label instead.

I also think it's easier if you create a new command for placing your nodes which reduces 3 lines of code for one node, to one line of code, using the following syntax:

\fnode[optional]{bottom left coord}{top right coord}{text}

Output

figure 1

Code

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{
    block/.style 2 args = {
        draw, line width=1pt, inner sep=0, outer sep=0,
        fit=(#1) (#2)}
}

\newcommand{\fnode}[4][]{
    \coordinate (bottom left) at (#2);
    \coordinate (top right) at (#3);
     \node[block={bottom left}{top right}, #1, label=center:#4] {};
}

\begin{document}
\begin{tikzpicture}[x=1mm,y=1mm]

\draw[step=1cm,gray,very thin] (-10,-10) grid (70,70);

\fnode{0,22.5}{14.4,42}{$R_{mki1}$}
\fnode{0,42}{14.4,49}{$R_{mr1}$}
\fnode{0,49}{14.4,59}{$R_{mko1}$}

\end{tikzpicture}
\end{document}

1: node positioned by fit: how to center a node text?
2: Wrong vertical text position with Tikz

3

This works perfect as long as your text doesn't get too long.

\documentclass[tikz, border=6mm]{standalone}

\begin{document}
  \begin{tikzpicture}[x=1mm, y=1mm]
    \coordinate[](0_r1) at (0,22.5);
    \coordinate[](0_r2) at (0,42);
    \coordinate[](0_r3) at (0,49);

    \coordinate[](b1_r2) at (14.4,42);
    \coordinate[](b1_r3) at (14.4,49);
    \coordinate[](b1_r4) at (14.4,59,0);

    \draw (0_r1) rectangle (b1_r2) node [pos=.5] {$R_{mki1}$};
    \draw (0_r2) rectangle (b1_r3) node [pos=.5] {$R_{mr1}$};
    \draw (0_r3) rectangle (b1_r4) node [pos=.5] {$R_{mko1}$};
  \end{tikzpicture}
\end{document}

rendered image

1
  • equally good solution, in some case probably even more versatile! The other answer offers more explanations though, Thank you! Commented Jul 14, 2015 at 11:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .