3

I would like to annotate a table with an arrow along the right side representing (in this case) decreasing operator precedence for a computer language. I am familiar with the other solution listed here, which involves placing the entire table in math mode, but I'd like to have a bit more control over the formatting of the text and arrow, as well as perhaps have a nice colored tikz arrow with the text inside of it.

My current example uses math mode inside of a /rotatebox, but the arrow is not column height, as I would like.

\documentclass[10pt]{article}
\usepackage[portrait, margin=0.25in]{geometry}
\usepackage{inconsolata}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\ttdefault}
\usepackage[fleqn]{amsmath}
\usepackage{booktabs}
\usepackage{multirow}

\begin{document}
\begin{tabular}{@{}l|llllllll@{}}
    Unary & +  & -  & \textasciicircum &&&&&\multirow{6}*{\rotatebox[origin=c]{270}{$\underrightarrow{precedence}$}}\\
    Multiplication & * & / & \% & << & >> & \& & \&\textasciicircum&\\
    Addition & + & - & | & \textasciicircum &&&&\\
    Comparison & == & != & < & <= & > & >= &&\\
    Logical & \&\& &&&&&&&\\
            & ||   &&&&&&&
\end{tabular}
\end{document}

Currently looks like this: example output

Interested in something like this: potential output?

1 Answers1

1

This is very easy to get with pst-node (a member of the pstricks family): put the table in a postscript environment, define empty nodes at the end of the first and last row, and connect them with an arrow. Furthermore the text over the arrow is noted in math mode, but as a real text:

\documentclass[10pt]{article}
\usepackage[portrait, margin=0.25in]{geometry}
\usepackage{inconsolata}
\usepackage[T1]{fontenc}
\renewcommand*\familydefault{\ttdefault}
\usepackage[fleqn]{amsmath}
\usepackage{booktabs}
\usepackage{multirow, rotating}
\usepackage{pst-node, auto-pst-pdf}

\begin{document}

\renewcommand\arraystretch{1.25}
\begin{postscript}
\begin{tabular}{@{}l|llllllll@{}}
    Unary & + & - & \textasciicircum &&&&&\pnode{B}\\%
    Multiplication & * & / & \% & << & >> & \& & \&\textasciicircum&\\
    Addition & + & - & | & \textasciicircum &&&&\\
    Comparison & == & != & < & <= & > & >= &&\\
    Logical & \&\& &&&&&&&\\
            & || &&&&&&&\pnode{E}
\end{tabular}
\psset{arrowinset=0.12, arrows=->, nrot=:U, shortput=nab, labelsep=2pt}
\ncline[linecolor=red, nodesepA=-10.5pt, nodesepB=-4.8pt]{B}{E}^{\colorbox{Pink3!60}{precedence}}
\ncline[offset=30pt, linecolor=blue ]{B}{E}\ncput*{\colorbox{LightSteelBlue1}{precedence}}
\end{postscript}

\end{document} 

enter image description here

Added: This other code gives an automatic sizing of the arrow. it puts the tabular in a box node, to which are associated 12 secondary nodes,and two of the rightmost associated nodes are connected by an arrow:

\begin{postscript}
\psDefBoxNodes{T}{
\begin{tabular}{@{}l|llllllll@{}}
    Unary & + & - & \textasciicircum &&&&& \\%
    Multiplication & * & / & \% & << & >> & \& & \&\textasciicircum&\\
    Addition & + & - & | & \textasciicircum &&&&\\
    Comparison & == & != & < & <= & > & >= &&\\
    Logical & \&\& &&&&&&&\\
            & || &&&&&&&
\end{tabular}}
\psset{arrowinset=0.12, arrows=->, nrot=:U, labelsep=2pt}
\pcline[linecolor=red](T:tr)(T:br)
\naput[nrot=:U]{\colorbox{Pink3!30}{precedence}}%{T:Cr}
\end{postscript}

enter image description here

Bernard
  • 271,350
  • Very nice! Still not quite as tall as the table, though. How can I get it to extend to as close to the length of the vertical divider as I can? – Flowchartsman Jan 26 '16 at 02:09
  • Yes, the nodes are set on the base line of the row. I'll update my answer in a moment. – Bernard Jan 26 '16 at 02:21
  • Also, is there a way to specify the background color for \ncput* ? Currently it is white, and the full example is on a colored background. I understand that, from the way the additive rendering works, there's no such thing as a "transparent" background, so specifying the color directly seems as if it should suffice. – Flowchartsman Jan 26 '16 at 02:25
  • Aha, never mind on the background color. 'fillcolor=' inside of \psset has done the trick nicely! – Flowchartsman Jan 26 '16 at 02:35
  • I've updated my answer. The adjustment of the arrows length has to be done by hand. Is that what you want? – Bernard Jan 26 '16 at 03:22
  • Yep! I ended up tweaking the x and y of the nodes themselves at instantiation, but basically the same thing. Works a peach! Thanks! – Flowchartsman Jan 26 '16 at 03:31
  • @Alaska: I've just added an automatic solution. – Bernard Jan 26 '16 at 11:12
  • Even nicer! I'd give you two checks if I could. – Flowchartsman Jan 26 '16 at 15:25