4

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.

  • 1
    Did you considered using the listings package or the minted package? I don't know but if you are using pointers, references and want a C++-like syntax this would be my first suggestion? listings documentation Perhaps it is too much for programming code and doesn't fit your needs precisely?! – patrickvogt Apr 17 '13 at 16:41
  • You should take a look at the listings documentation, section 1.5 Alternatives. There is a short description of similar packages. Perhaps there is a package which fits for your use case – patrickvogt Apr 17 '13 at 16:44
  • The algorithm I'm writing up is not specific to C/C++, so I am avoiding language-specific syntax. I'm also trying to keep the writeup focused on the algorithm, instead of implementation details. Otherwise, listings/minted look like they'd be great. I'll need to keep them in mind for future projects. – ɲeuroburɳ Apr 17 '13 at 16:47
  • If you want it to be general (no language-specific syntax), you could create a custom function address(root) or something similar, that way you do it totally generic. But think long and hard about the clarity of such a function, maybe address is not the correct name in your context. – zeroth Apr 17 '13 at 16:51
  • @ɲeuroburɳ: You don't have to use implementation details in the listings package. It was just my suggestion. So you can drop any implementation details such as data types and built in (language-specific) functions. You could also define your own language with both packages and define your own syntax highlighting (but I think this is quite an overhead) just for one algorithm – patrickvogt Apr 17 '13 at 16:57
2

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}
  • Nice set of options. Trying them out now. As you astutely bring up, I am in fact using what amounts to both . and ->. Currently I am using subscripts for ., but that can always be changed too. :) – ɲeuroburɳ Apr 17 '13 at 17:14

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.