0

I use Mac, OSX 10.11.5. When I paste text from pdf files to emacs (tex mode), I get characters like "∈", "Ω". I need to convert them to LaTex equivalents "\in" "\Omega". Any hint?

  • 2
    In case you are willing to change: In TeXstudio there is a function Convert to LaTeX which does exactly this. – user36296 Jun 27 '16 at 9:35
  • Do you want to know how to convert into \in or if/how you could directly use in your .tex file? – ebosi Jun 27 '16 at 9:36
  • I use plain latex. I need to convert unicode characters to their latex equivalents, in order to have a collection of ASCII tex files. I should like to use emacs or python – Flavio Sartoretto Jun 27 '16 at 9:44
  • Then I think you could write a python script with a home-made lexicon as described here on stackexchange. I am not aware of a special characters -> LaTeX command lexicon, except this one... but I think you would prefer to make your own that you complete when needed! – ebosi Jun 27 '16 at 9:57
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – user36296 Jul 25 '16 at 16:55
1

Using suggestions form various editors (thanks a lot!) I implemented this solution, which is ok for me

#!/usr/bin/pythonw
# -*- coding: utf-8 -*-

import sys
import re

# NOTE: literals enclosing backckslash are forced to raw using prefix r'...'
repldict = {'Ω':r'\Omega','?<8A><86>':r'\subseteq','?<8A><82>':r'\subset',
        '?<9F>?':'<','?<9F>?':'>',
        '?<88><88>':r'\in','?<97>':r'\times','?<80><99>':'*apostrofo*',
        'μ':r'\mu','λ':r'\lambda','?<86>':r'\phi',
        '?<86><92>':r'\rightarrow','·':r'\cdot','?<88>?':'||',
        '?<89>?':r'\le',
        '?<88><9E>':r'\infty','ε':r'\varepsilon','Φ':r'\Phi',
        '?<88><92>':r'-','?<80><9C>':r'``','?<80><9D>':r'"','?<80><94>':r'-'}
def replfunc(match):
return repldict[match.group(0)]

def main():
 regex = re.compile('|'.join(re.escape(x) for x in repldict))

 inFile = sys.argv[1]
 fin = open(inFile,'r')
 outFile = 'pdf2latexChars' + '.tex'
 fout = open(outFile, 'w')
 print 'inFile=' + inFile + '; outFile=' + outFile 

 for line in fin:
    fout.write(regex.sub(replfunc,line))

if __name__ == '__main__':
 main()
0

I just did this in PHP. You should be able to do something like this in Python:

$string = "I want the Latex equivalent of Ω, Δ, etc.";
$string = str_replace(
array("Ω","Δ"),
array("$\Omega$", "$\Delta$"),
$string);

string variable will now be: "I want the Latex equivalent of $\Omega$, $\Delta$, etc."

  • This doesn't solve the problem, in my opinion. – Werner Jul 3 '16 at 16:36
  • Well, he mentioned that he would like to use Python to solve the problem. Using this as reference should be sufficient? – SwCharlie Jul 4 '16 at 16:29

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.