2

I wonder if there is any way I can put arrows in the table like in the picture below?enter image description here

The LaTeX code that I use is the following:

\setlength{\arrayrulewidth}{0.3mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.3}
\begin{longtable}{|c|c|c|c|c|}
    \hline
    \textbf{$p$}    &    $=$    &   \textbf{$a$}    & \textbf{$+$}  & \textbf{$b$}  \\
    \hline
    \endfirsthead
    \multicolumn{5}{c}%
    {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
    \hline
    \textbf{$p$}    &       &   \textbf{$a$}    & $+$   & \textbf{$b$}  \\
    \hline
    \endhead
    \hline \multicolumn{5}{r}{\textit{Continued on next page}} \\
    \endfoot
    \hline
    \endlastfoot
                    & $=$           & $0$       &           & $p$   \\
                    & $=$           & $1$       &           & $p-1$ \\
                    & $=$           & $2$       &           & $p-2$ \\
    \textbf{$p$}    & $\vdots$      & \vdots    & \vdots    & \vdots\\
                    & $=$           & $p-2$     &           & $2$   \\
                    & $=$           & $p-1$     &           & $1$   \\
                    & $=$           & $p$       &           & $0$   \\
\end{longtable}

which gives me the following output:

enter image description here

I hope someone can help me with this one.

2
  • 1
    Welcome to TeX SX! What if the connected cells appear on different pages?
    – Bernard
    Jul 14, 2018 at 12:22
  • I need it to be exactly like the first picture. Jul 14, 2018 at 12:25

3 Answers 3

4

A simple code with pstricks: I add a column of (empty) nodes and connect these nodes with \ncbar:

\documentclass{article}
\usepackage{array} 
\usepackage{longtable} 
\usepackage{pst-node, auto-pst-pdf}

\begin{document}

\setlength{\arrayrulewidth}{0.3mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.3}
\begin{postscript}
\psset{arrows=->, arrowinset=0.12, linearc=0.05}
\newcommand{\mynode}[1]{\pnode[0pt,0.5ex]{#1}}
\begin{longtable}{|c|c|c|c|@{\qquad}c@{\qquad}c|}
    \hline
    \textbf{$p$} & $=$ & \textbf{$a$} & \textbf{$+$} & \textbf{$b$} & \qquad\\
    \hline
    \endfirsthead
    \multicolumn{5}{c}%
    {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
    \hline
    \textbf{$p$} & & \textbf{$a$} & $+$ & \textbf{$b$} \\
    \hline
    \endhead
    \hline \multicolumn{5}{r}{\textit{Continued on next page}} \\
    \endfoot
    \hline
    \endlastfoot
                    & $=$ & $0$ & & $p$ & \mynode{p} \\
                    & $=$ & $1$ & & $p-1$ & \mynode{p1} \\
                    & $=$ & $2$ & & $p-2$ & \mynode{p2} \\
    \textbf{$p$} & $\vdots$ & \vdots & \vdots & \vdots & \\
                    & $=$ & $p-2$ & & $2$ & \mynode{2} \\
                    & $=$ & $p-1$ & & $1$ & \mynode{1} \\
                    & $=$ & $p$ & & $0$ & \mynode{0} \\
\end{longtable}
\ncbar[arm=1.2cm]{p}{0}
\ncbar[arm=1.6cm]{p1}{1}
\ncbar[arm=2.2cm]{p2}{2}
\end{postscript}

\end{document} 

enter image description here

4

Similar to @marmot's but I clean these noisy cross-overs and use a virtual column for horizontally aligning the beginnings and ends of arrows.

\documentclass{article}%
\usepackage{array}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt]\node(#1){$#2$};
\else
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt]\node(#1){#2};%
\fi}

\begin{document}
\setlength{\arrayrulewidth}{0.3mm}
\setlength{\tabcolsep}{12pt}
\renewcommand{\arraystretch}{1.3}
\begin{tabular}{|>{\centering}p{1.8cm}|c|>{\centering}p{1.8cm}|c|>{\centering}p{1.8cm}|@{}p{0pt}@{}} % 
    \hline
    \textbf{$p$}    &    $=$    &\textbf{$a$} & \textbf{$+$} & \textbf{$b$} & \tabularnewline
    \hline
    \textbf{$p$}    &           &\textbf{$a$} & $+$     & \textbf{$b$}  & \tabularnewline
    \hline
                    & $=$       & $0$       &           & $p$      &\tikznode{p2}{} \tabularnewline
                    & $=$       & $1$       &           & $p-1$    &\tikznode{p1}{} \tabularnewline
                    & $=$       & $2$       &           & $p-2$    &\tikznode{p0}{} \tabularnewline
    \textbf{$p$}    & $\vdots$  & $\vdots$  & $\vdots$  & $\vdots$ &\tabularnewline
                    & $=$       & $p-2$     &           & $2$      &\tikznode{0}{}  \tabularnewline
                    & $=$       & $p-1$     &           & $1$      &\tikznode{1}{}  \tabularnewline
                    & $=$       & $p$       &           & $0$      &\tikznode{2}{}  \tabularnewline
  \hline                    
\end{tabular}
\begin{tikzpicture}[overlay,remember picture,line width=.7pt,rounded corners]
 \foreach \X in {0,1,2}
  \draw[-latex] ($(p\X)+(-.7,3pt)$) -- ++({1.2+0.5*\X},0) |- ($(\X)+(-.7,3pt)$);
\end{tikzpicture}
\end{document}

enter image description here

0
3

Rather straightforward with TikZ as long as the table is on one page only.

\documentclass{article}%
\usepackage{longtable}
\usepackage{tikz}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}

\begin{document}
\setlength{\arrayrulewidth}{0.3mm}
\setlength{\tabcolsep}{18pt}
\renewcommand{\arraystretch}{1.3}
\begin{longtable}{|c|c|c|c|c|}
    \hline
    \textbf{$p$}    &    $=$    &   \textbf{$a$}    & \textbf{$+$}  & \textbf{$b$}  \\
    \hline
    \endfirsthead
    \multicolumn{5}{c}%
    {\tablename\ \thetable\ -- \textit{Continued from previous page}} \\
    \hline
    \textbf{$p$}    &       &   \textbf{$a$}    & $+$   & \textbf{$b$}  \\
    \hline
    \endhead
    \hline \multicolumn{5}{r}{\textit{Continued on next page}} \\
    \endfoot
    \hline
    \endlastfoot
                    & $=$           & $0$       &           & \tikznode{p0}{$p$}   \\
                    & $=$           & $1$       &           & \tikznode{p1}{$p-1$} \\
                    & $=$           & $2$       &           & \tikznode{p2}{$p-2$} \\
    \textbf{$p$}    & $\vdots$      & \vdots    & \vdots    & \vdots\\
                    & $=$           & $p-2$     &           & \tikznode{2}{$2$}   \\
                    & $=$           & $p-1$     &           & \tikznode{1}{$1$}   \\
                    & $=$           & $p$       &           & \tikznode{0}{$0$}   \\
\end{longtable}
\begin{tikzpicture}[overlay,remember picture]
 \foreach \X in {0,1,2}
 { \draw[-latex] (p\X) -- ++({1.5+0.5*\X},0) |- (\X);}
\end{tikzpicture}
\end{document}

enter image description here

And here is a version which is closer to your upper picture and also does not use longtable as this is not necessary here.

\documentclass{article}%
\usepackage{array}
\usepackage{tikz}
\newcommand{\tikznode}[2]{\relax
\ifmmode%
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};
\else
  \tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}

\begin{document}
\setlength{\arrayrulewidth}{0.3mm}
\setlength{\tabcolsep}{12pt}
\renewcommand{\arraystretch}{1.3}
\begin{tabular}{|>{\centering}p{1.8cm}|c|>{\centering}p{1.8cm}|c|>{\centering}p{1.8cm}|} % 
    \hline
    \textbf{$p$}    &    $=$    &   \textbf{$a$}    & \textbf{$+$}  & \textbf{$b$}  \tabularnewline
    \hline
    \textbf{$p$}    &       &   \textbf{$a$}    & $+$   & \textbf{$b$}  \tabularnewline
    \hline
                    & $=$           & $0$       &           & \tikznode{p0}{$p$}   \tabularnewline
                    & $=$           & $1$       &           & \tikznode{p1}{$p-1$} \tabularnewline
                    & $=$           & $2$       &           & \tikznode{p2}{$p-2$} \tabularnewline
    \textbf{$p$}    & $\vdots$      & $\vdots$    & $\vdots$    & $\vdots$ \tabularnewline
                    & $=$           & $p-2$     &           & \tikznode{2}{$2$}   \tabularnewline
                    & $=$           & $p-1$     &           & \tikznode{1}{$1$}   \tabularnewline
                    & $=$           & $p$       &           & \tikznode{0}{$0$}   \tabularnewline
  \hline                    
\end{tabular}
\begin{tikzpicture}[overlay,remember picture]
 \foreach \X in {0,1,2}
 { \draw[-latex] (p\X) -- ++({1.8+0.5*\X},0) |- (\X);}
\end{tikzpicture}
\end{document}

enter image description here

8
  • Thank you so much for your help! I'm still learning using tikz. Again, thanks a lot! Jul 14, 2018 at 12:36
  • To make the output look more like the figure in the OP you could give the \tikznodes a minimum width...
    – gebruiker
    Jul 14, 2018 at 12:41
  • @gebruiker I would not redefine \tikznode for that but just make the column wider.
    – user121799
    Jul 14, 2018 at 12:45
  • @AmirulAizad If that answer works for you, could you perhaps consider accepting it by clicking on the checkmark left of it?
    – user121799
    Jul 14, 2018 at 12:48
  • But then the arrows would still be touching the node text. I added minimum width=12ex to the nodes and changed the 1.5 to 2.5 in the \draw command. This looks more like the OP. The OP can now still make his columns wider if he wants.
    – gebruiker
    Jul 14, 2018 at 12:48

You must log in to answer this question.

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