[Tex/LaTex] Algorithm pointers and references

algorithms

I'm looking for a clean way/best-practice of writing an algorithmic block that uses pointers and references. I've had success with algorithmic in the past, but this is the first time I've tried writing up something with pointers. The snippet code in C/C++ looks like:

Node *n = &root;
n->value = input;

The LaTeX approach I attempted doesn't look quite right:

\STATE $n \leftarrow \& \mathrm{root}$
\STATE $n \rightarrow \mathrm{value} \leftarrow \mathrm{input}$

Which renders as:

latex pointers and references

I think it looks a little odd with both with the &, and the left and right arrows making it look like two things are pointing to mathrm. It seems a bit like the problem is a conflict with the \leftarrow being used for assignment, but \leftarrow appears to be common practice, so I'd like to follow it.

Of course I can also rewrite the algorithm to NOT use pointers, but it inflates the code block almost 2x and I'm working against a page limit. 🙂 The end goal here of course is readability.

Best Answer

Using leftarrow for assignment makes using right arrow a bit problematic as you note. A couple of suggestions here, I quite like the subscript version but it depends if you need . and -> in the C or other uses of subscript. I also made the & smaller because I don't like the big one:-) If you do keep \rightarrow you should probably use {\rightarrow} so it uses closer spacing and in particular different spacing to the assignment.

enter image description here

\documentclass{article}

\usepackage{algorithmic}

\begin{document}

\begin{algorithmic}
\STATE $n \leftarrow \& \mathrm{root}$
\STATE $n \rightarrow \mathrm{value} \leftarrow \mathrm{input}$
\STATE
\STATE $n \leftarrow  {\scriptstyle\&}\mathrm{root}$
\STATE $n _\mathrm{value} \leftarrow \mathrm{input}$
\STATE
\STATE $n \leftarrow {\scriptstyle\&} \mathrm{root}$
\STATE $n {\scriptscriptstyle\searrow} \mathrm{value} \leftarrow \mathrm{input}$


\end{algorithmic}

\end{document}