9

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

\variable

for the variable of functions so that I can write

$f(\variable)$

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)$.)

1

1 Answer 1

11

You can make x math active:

\documentclass{article}
\usepackage{xcolor}

%%% 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
\begingroup\lccode`~=`x
\lowercase{\endgroup\def~}{{\textcolor{red}{\variable}}}
\AtBeginDocument{\mathcode`x=\string"8000 }

%%% When you're through, remove the lines
%%% above and use the following one
%\newcommand{\variable}{x}


\begin{document}

This is good: $f(\variable)$

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

\end{document}

enter image description here

4
  • 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. Aug 19, 2013 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? Aug 20, 2013 at 7:02
  • 1
    @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, 2013 at 8:38
  • thanks! But I can also adapt to $a_{x}$, indeed... Aug 20, 2013 at 10:01

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.