[Tex/LaTex] How to format mathematical and programming variables in text

algorithmsbest practicesmath-mode

I am describing an algorithm in a journal paper. In this algorithm I have a few variables. I gave them the same names as in my source code (C++ source of the algorithm, not LaTeX source). So I have variables such as:

  • new (new node that is being inserted)
  • reference (arbitrarily chosen node)
  • x (generic looping variable)

In computer programming it is good practice to give variables meaningful names. However when reading other articles describing algorithms I often see abbreviated names. In my case it would be:

  • n
  • r
  • x

Is this a good practice? Is there a difference between programming and mathematical texts?

Another thing is the mode in which to write these variables in text and in an algorithm environment. Should I use:

  • $new$
  • \emph{new}
  • \textbf{new}
  • \textit{new}

Or perhaps use one convention in inline text and a different one in equations and algorithm environments?

I couldn't find any relevant information in the editor's manual for the journal and also nothing relevant in the LaTeX class manual. All they say is to make variables italic, so that they are not confused with units (for example W can be confused with Watt).

Best Answer

You have asked two separate (interesting) questions.

The first is about names. In general, mathematicians prefer generic names ("n", "r", x") while programmers prefer longer, more meaningful ones. In your case I'd go for meaningful.

Your second question is about typography. There I think the computer programmer's practice of delayed commitment will serve you well. Write a macro

\newcommand{\variable}[1]{%
...  convention for typesetting #1 ...
}

and use \variable{new} in your document. Then you can change the formatting later to whatever you (or your editor) likes. See the comments above for some typographical do's and don't's.

You could even postpone committing to variable names with macros like

\newcommand{\reference}{\variable{reference}}

one for each variable name. If there were lots of those you could have a macro to generate the macros from a list.