TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I do not know if this question has more to do with the editor I use (which is LaTeXShop, on Mac) or some clever LaTeX package, but I am typesetting some notes for a course and I would like to systematically use a command, say


for the variable of functions so that I can write


and get $f(x)$ or $f(Y)$ consistently in the whole document by simply choosing once and for all in the preamble the value of \variable. The reason is that in a first version $f(x)$ might be good, but I think I will eventually want to move to different variable names. The problem is that I have been typing $f(x)$ automatically for ages now, and I fear that here and there I will write $f(x)$ instead of $f(\variable)$ getting the same visual effect if \variable is set to $x$, so that I will never catch the error while proof reading, but creating a mess the day I will rename my \variable. So comes my question: is there a systematic way to ask LaTeX to warn me whenever I use $x$ "although I wanted to use \variable"? Of course, I do not want it to warn me each time I use the letter x. (I will be writing in French, and there are a lot of x's here and there in text) nor to warn me about $f(x)$ and forget about $g(x)$.)

share|improve this question
You can write \newcommand*{\variable}{x} and later change it to \newcommand*{\variable}{Y}. There are ways to warn about an x in math-mode by making it active but is this really something you want? Why don’t you search once for xs in math-mode and get rid of them? See A lightweight editor that supports search and replace in math mode – Qrrbrbirlbel Aug 19 '13 at 3:10
up vote 10 down vote accepted

You can make x math active:


%%% The following four lines will make all
%%% explicit `x' in math mode being printed red
\edef\variable{\mathchar\the\mathcode`\x\relax} % this indirectly defines \variable to print an x
\AtBeginDocument{\mathcode`x=\string"8000 }

%%% When you're through, remove the lines
%%% above and use the following one


This is good: $f(\variable)$

This is bad: $f(x)\ne g(x)$.


enter image description here

share|improve this answer
Great, thank you! This is exactly what I was looking for. It would be very kind if you could also explain what is going on: in particular, I do not understand where in your code you told LaTeX that $\variable$ corresponds to x, because it is commented in the version I see. – Filippo Alberto Edoardo Aug 19 '13 at 9:26
I tried your code and it is working, although it gives me a bizarre error when I write $x$ as subscript (which is not a big issue, since the point was precisely to remove all these $x$; but can still be annoying if, for any reason, I want to use the $x$-symbol elsewhere...). Any clue? – Filippo Alberto Edoardo Aug 20 '13 at 7:02
@FilippoAlbertoEdoardo With the code I presented, $a_x$ will indeed give an error, which is not raised when the “more correct” syntax $a_{x}$ is used. I'll change the code to allow using $a_x$. – egreg Aug 20 '13 at 8:38
thanks! But I can also adapt to $a_{x}$, indeed... – Filippo Alberto Edoardo Aug 20 '13 at 10:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.