Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to convert characters of type á ê õ ção to \'a \^e \~o \c{c} \~a automatically?

share|improve this question
11  
Why do you need to do this? If you save your source file as UTF-8, and use \usepackage[utf8]{inputenc} you can keep them in your source. –  Alan Munn Jul 18 '11 at 17:24
2  
Also, the answer likely depends on how you'd like to do it. In your editor? In LaTeX? In a preprocessing step? –  You Jul 18 '11 at 17:31
    
Alan Munn, i use [utf8]{inputenc}, but is possible convert directly in edit file.tex? –  Regis da Silva Jul 18 '11 at 17:39
    
if you use an editor that has automatic spellchecking rules, you can probably define new rules that would do that. The same way i is often automatically changed to I. But a detailed answer is highly dependent on the editor you're using. –  Lexiel Jul 18 '11 at 18:02
3  

3 Answers 3

You probably want to keep a version of your document with the unreplaced characters as it is much easier to read. If you use makefiles to process your documents, you might write something along these lines

#! -*- coding: utf-8 -*-

SHELL = /bin/sh
DOCUMENT = doc

$(DOCUMENT).pdf : $(DOCUMENT).tex
    cp $(DOCUMENT).tex temp_$(DOCUMENT).tex
    sed -i "s/é/\\\'{e}/g" temp_$(DOCUMENT).tex
    sed -i 's/ç/\\c{c}/g' temp_$(DOCUMENT).tex
    # more substitutions to add...
    pdflatex temp_$(DOCUMENT).tex
    cp temp_$(DOCUMENT).pdf $(DOCUMENT).pdf

Actually all my makefiles copy doc.tex to temp_doc.tex before doing anything. This way, I can easily clean up any machine generated files via rm -f temp*.

However, are you really sure you want to do this? Replacing these characters with their respective macros doesn't really give you any advantage. (At least none I could readily see.) But it comes at the cost of poor kerning. (See also section 2.2.6 of l2tabuen.)

Best

share|improve this answer

Here is a quick python script that does the trick, it handles both combining accents as well as pre-composed characters, however it takes only a string of text and some extra work is needed to handle complete TeX files:

#!/usr/bin/env python
import unicodedata
import sys

accents = {
    0x0300: '`', 0x0301: "'", 0x0302: '^', 0x0308: '"',
    0x030B: 'H', 0x0303: '~', 0x0327: 'c', 0x0328: 'k',
    0x0304: '=', 0x0331: 'b', 0x0307: '.', 0x0323: 'd',
    0x030A: 'r', 0x0306: 'u', 0x030C: 'v',
}

def uni2tex(text):
    out = ""
    txt = tuple(text)
    i = 0
    while i < len(txt):
        char = text[i]
        code = ord(char)

        # combining marks
        if unicodedata.category(char) in ("Mn", "Mc") and code in accents:
            out += "\\%s{%s}" %(accents[code], txt[i+1])
            i += 1
        # precomposed characters
        elif unicodedata.decomposition(char):
            base, acc = unicodedata.decomposition(char).split()
            acc = int(acc, 16)
            base = int(base, 16)
            if acc in accents:
                out += "\\%s{%s}" %(accents[acc], unichr(base))
            else:
                out += char
        else:
            out += char

        i += 1

    return out

if __name__ == '__main__':
    t = unicode(sys.argv[1], "utf-8")
    print(uni2tex(t))

and invoked as:

$ python uni2tex.py "á ê õ ção ̆ a ă ̆a"

which outputs \'{a} \^{e} \~{o} \c{c}\~{a}o \u{ }a \u{a} \u{a}.

share|improve this answer

If you use emacs, use the iso- functions. In your case: iso-iso2tex. I posted more details in a previous answer: emacs-accented-letters

share|improve this answer

Your Answer

 
discard

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.