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'd like to have two boxes with text inside them atop one another with some vertical space, and with two parallel vertical edges linking them. With just one such linking edge, tikz makes it easy: \draw (node1) -- (node2); (where node1 and node2 specify the two boxes with text). There's got to be a bit of separation between them; however, I've tried using things like (node1) +(5pt,0) -- (node2) +(5pt,0) and frankly it isn't working. I've tried scoping with shift={(5pt,0),transform shape} and the shift was ignored. So, how should I do this? (FYI: there's addtional options on the edges specifying labels and arrows, but I don't believe that they're relevant to this).

Example of single link:

\documentclass[12pt]{article}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}[yscale=-1]
    \node at (0,0) (node1) [shape=rectangle,fill=green] {box1};
    \node at (0,1) (node2) [shape=rectangle,fill=green] {box2};
    \draw (node1) -- (node2);
  \end{tikzpicture}
\end{document}

An ascii version...

.  +----+
.  |box1|
.  +----+
.   |  |
. 1 v  ^ 3
.   |  |
.  +----+
.  |box2|
.  +----+

(pls ignore the "."s, I needed them to get the code entry to work)

Thank you for your solutions! I'll definitely be trying them out.

share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Jan 13 at 19:46

5 Answers 5

up vote 9 down vote accepted

If I understand the problem correctly, you may want to use the double and double distance keys.

As described in subsubsection 15.3.5 of the tikz documentation, using the double key in a \draw command produces a double line. You can also fine-tune the distance between the two lines with the double distance key; using the latter automatically enables double, as pointed out in Claudio's comment. See below.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}

\begin{document}

 \begin{tikzpicture}[yscale=-1]
    \node at (0,0) (node1) [shape=rectangle,fill=green] {box1};
    \node at (0,1) (node2) [shape=rectangle,fill=green] {box2};
    \draw[double distance=5pt] (node1) -- (node2);
    \node at (0,2) (node3) [shape=rectangle,fill=green] {box1};
    \node at (0,3) (node4) [shape=rectangle,fill=green] {box2};
    \draw[double distance=10pt] (node3) -- (node4);
  \end{tikzpicture}

\end{document}

Edit: if you need different decorations, labels etc. for the two lines, you need to construct two distinct paths; see below. To draw an arrowhead at the middle of an path, I refer you to Alain Matthes's answer to Tikz: Arrowheads in the center.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}

\tikzset{
    ->-/.style={decoration={
        markings,
        mark=at position .5 with {\arrow{>}}},postaction={decorate}},
    -<-/.style={decoration={
        markings,
        mark=at position .5 with {\arrow{<}}},postaction={decorate}},
}

\begin{document}

\def\mydbldist{5pt}
 \begin{tikzpicture}[yscale=-1]
    \node at (0,0) (node1) [shape=rectangle,fill=green] {box1};
    \node at (0,1) (node2) [shape=rectangle,fill=green] {box2};
    \draw[->-] ([xshift=-.5*\mydbldist]node1.south) -- ([xshift=-.5*\mydbldist]node2.north)
        node[pos=.5,anchor=east] {$1$};
    \draw[-<-] ([xshift=.5*\mydbldist]node1.south) -- ([xshift=.5*\mydbldist]node2.north)
        node[pos=.5,anchor=west] {$3$};
  \end{tikzpicture}

\end{document}
share|improve this answer
1  
There are decorations needed on each of the edges, so simply doubling the lines isn't quite there. I'm going to try to include an ascii version... –  user21497 Jan 12 at 19:35
    
+----+ |box1| +----+ | | 1 v ^ 3 | | +----+ |box2| +----+ –  user21497 Jan 12 at 19:35
    
For double paths, you don't need double option if you specify double distance: the latter automatically enables the double mode on the path. –  Claudio Fiandrino Jan 12 at 20:08
    
@ClaudioFiandrino I didn't know that, but it makes sense :) I'll edit my answer. –  Jubobs Jan 12 at 20:10

Just to show another option. You can use calc library and calculate the points on the edges by something like

($(node1.south west)!0.33!(node1.south east)$)

which is 0.33 (of the total bottom edge length) from the south west of node1. This way your points are more accurate.

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{decorations.markings,calc}

\tikzset{
    ->-/.style={decoration={
        markings,
        mark=at position .5 with {\arrow{>}}},postaction={decorate}},
    -<-/.style={decoration={
        markings,
        mark=at position .5 with {\arrow{<}}},postaction={decorate}},
}

\begin{document}

\def\mydbldist{5pt}
 \begin{tikzpicture}[yscale=-1]
    \node at (0,0) (node1) [shape=rectangle,fill=green] {box1};
    \node at (0,1) (node2) [shape=rectangle,fill=green] {box2};
    \draw[->-] ($(node1.south west)!0.33!(node1.south east)$) -- ($(node2.north west)!.33!(node2.north east)$)
        node[pos=.5,anchor=east] {$1$};
    \draw[-<-] ($(node1.south west)!0.66!(node1.south east)$) -- ($(node2.north west)!.66!(node2.north east)$)
        node[pos=.5,anchor=west] {$3$};
  \end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

My (not so minimal) example is a bit more hackish: you draw manually the two lines exploiting the nodes automatically created when you draw a node shaped as a rectangle (refer to page 420 of the PGF manual). You shift your position from those "auxiliary" nodes with [xhift=...,yshift=...].

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{figure}[h]
\centering
\begin{tikzpicture}[block/.style={rectangle,draw,minimum height=1cm,text width=5cm,align=center}]
\node (b1) [block] {box1};
\node (b2) [block,below=of b1] {box2};
  \begin{scope}
    \draw [->] ([xshift=-.5cm]b1.south) -- ([xshift=-.5cm]b2.north);
    \draw [->] ([xshift=.5cm]b1.south) -- ([xshift=.5cm]b2.north);
  \end{scope}
\end{tikzpicture}
\caption{caption}
\label{fig:boxes}
\end{figure}
\end{document}

This solution lets you add arrow heads to your lines. enter image description here

share|improve this answer

Next code shows an alternative syntax to the one provided by Pier Paolo: (node.angle)

This coordinate refers to a point of the node’s border where a ray shot from the center in the given angle hits the border

With this syntax it's easy to draw parallel lines between node borders.

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

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[%
    block/.style={rectangle,fill=green,minimum height=1cm,text width=2cm,align=center}]
\node[block] (node1) {box1};
\node[block, above=1cm of node1] (node2) {box2};
    \draw [->] (node1.70) -- (node2.-70) node [pos=0.5,right] {1};
    \draw [<-] (node1.110) -- (node2.-110) node [pos=0.5,left] {3};
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

With PSTricks. No need to specify the absolute coordinates!

\documentclass[preview,border=12pt,12pt]{standalone}
\usepackage{pstricks-add}

\begin{document}
\nointerlineskip
\begin{psmatrix}[rowsep=1cm,fillstyle=solid,fillcolor=green]
    \psframebox*{Box 1}\\
    \psframebox*{Box 2}
\end{psmatrix}
\psset{offset=6pt,ArrowInside=->}
\ncline{1,1}{2,1}\naput{3}
\ncline{2,1}{1,1}\naput{1}
\end{document}

enter image description here

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.