0

I would like the nodes created in the table to be connected by a line. Ideally, the lines connect from the right-middle edge of the border for the nodes on the left, and the left-middle edge of the border for the nodes on the right. I've included the node connection listing at the end of the code. You can see that it clearly does not map properly onto the table.

\documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{array}
\newcommand\mybox[2]{\tikz[remember picture]{\node (#2) [draw=black, inner sep=4pt, anchor=text, rounded corners=1mm] {#1};}}
\setlength{\tabcolsep}{0.5in}
\setlength{\extrarowheight}{0.3in}
\begin{document}
\begin{tabular}{rl}
\mybox{Molecular}{molecular} & \mybox{Diamond}{diamond} \\
\mybox{Amorphous}{amorph} & \mybox{Sodium}{na} \\
\mybox{Metallic}{metal} & \mybox{Carbon Dioxide}{carbondiox} \\
\mybox{Covalent Network}{covnet} & \mybox{Glass}{glass}
\end{tabular}
\begin{tikzpicture}
\draw (molecular) -- (carbondiox);
\draw (amorph) -- (glass);
\draw (metal) -- (na);
\draw (covnet) -- (diamond);
\end{tikzpicture}
\end{document}
  • 1
    You need to add [remember picture,overlay] to the tikzpicture environment – Salim Bou Mar 1 '17 at 7:32
  • @SalimBou This fixed the overlay issue, how to I adjust where the lines are connected on the node then? – J M Mar 1 '17 at 7:37
  • @SalimBou Nevermind, I got it – J M Mar 1 '17 at 7:42
  • 1
    Rather then (molecular) you can use (molecular.east) same with (carbondiox) change to (carbondiox.west) – Salim Bou Mar 1 '17 at 7:43
  • @SalimBou Yup, just got that figured out, but if you write up an answer with the fixed codeI will mark it as solved for you. Since you solved the more important piece. – J M Mar 1 '17 at 7:44
1

You need remember picture and overlay keys to your tikzpicture environment, to control where lines are connected just change (molecular) to (molecular.east ) or any anchor of node west, north, south, north east, north west, ...

\documentclass{article}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{array}
\newcommand\mybox[2]{\tikz[remember picture]{\node (#2) [draw=black, inner sep=4pt, anchor=text, rounded corners=1mm] {#1};}}
\setlength{\tabcolsep}{0.5in}
\setlength{\extrarowheight}{0.3in}
\begin{document}
\begin{tabular}{rl}
\mybox{Molecular}{molecular} & \mybox{Diamond}{diamond} \\
\mybox{Amorphous}{amorph} & \mybox{Sodium}{na} \\
\mybox{Metallic}{metal} & \mybox{Carbon Dioxide}{carbondiox} \\
\mybox{Covalent Network}{covnet} & \mybox{Glass}{glass}
\end{tabular}
\begin{tikzpicture}[remember picture,overlay]
\draw (molecular.east) -- (carbondiox.west);
\draw (amorph.east) -- (glass.west);
\draw (metal.east) -- (na.west);
\draw (covnet.east) -- (diamond.west);
\end{tikzpicture}
\end{document}  
| improve this answer | |
2

It is also feasible with pure TikZ without overlays.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
  column 1/.style={anchor=east},
  column 2/.style={anchor=west},
  ]
  \matrix[
  matrix of nodes,
  nodes={draw=black,inner sep=4pt,rounded corners=1mm},
  row sep=.5cm, column sep=1cm,
  ] (m) {
    Molecular        & Diamond        \\
    Amorphous        & Sodium         \\
    Metallic         & Carbon Dioxide \\
    Covalent Network & Glass          \\
  };
  \draw (m-1-1.east) -- (m-3-2.west);
  \draw (m-2-1.east) -- (m-4-2.west);
  \draw (m-3-1.east) -- (m-2-2.west);
  \draw (m-4-1.east) -- (m-1-2.west);
\end{tikzpicture}
\end{document}

enter image description here

| improve this answer | |
  • This is really nice to know about the table arrangement with tikz. I wish I was more proficient with it, it seems so powerful! – J M Mar 3 '17 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.