Automatically replace input with accents by their safe code for accents

I was checking this: Automatically replace open quote with accent, and it is actually similar to what I want. Since I'm starting with Emacs, and everything seems admissible to be automatized. Because I write spanish text, I type accents like for example á, é or í. I would like to have the instant automatic translation between the characters with an accent and their equivalent version using the slash form. So for instance, if I type á then it'll be translated to \'a and so on.

• It is no more annoying nowadays if you use utf8 encoding. All modern systems know about utf8. I've been typing accented letters without any problem for more than twenty years, and it makes the code much more readable. – Bernard Mar 21 '16 at 1:34
• I think this would be a step backward, really. We want people to use UTF-8 (and I hope you are when using Emacs!). And it is mostly a breeze to convert from one encoding to another (assuming it was encoded well to begin with). Even going from UTF-8 to the special world of TeX markup for a specific .tex file is not tricky if it should ever become necessary. – jon Mar 21 '16 at 4:36
• If you want, you can use M-x replace-string to change every accent after typeset. – Romain Picot Mar 24 '16 at 8:09

I'll echo what the commenters suggest and say that you would be better off writing the accents as you normally would and encoding the document in UTF-8:

\documentclass{article}
% Tells all the other packages that the document language is Spanish. It also  changes hyphenation rules and quotation marks. The choose the mexico or spain option depending on whether you want a period or a comma as a decimal separator in numbers.
\def\spanishoptions{mexico}
\usepackage[spanish]{babel}
% Encodes the input as UTF-8.
\usepackage[utf8]{inputenc}
% Encodes the fonts as T1
\usepackage[T1]{fontenc}

\begin{document}
ÁÉÍÓÚÜÑ áéíóúñ

\end{document}


See also the answers to the questions How to write directly French characters in Emacs and print them in LaTeX? and Why should I use \usepackage[T1]{fontenc}? for more information.