WUT Velma robot API
latex_equations.py
Go to the documentation of this file.
1 # Copyright (c) 2014, Robot Control and Pattern Recognition Group, Warsaw University of Technology
2 # All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are met:
6 # * Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # * Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
11 # * Neither the name of the Warsaw University of Technology nor the
12 # names of its contributors may be used to endorse or promote products
13 # derived from this software without specific prior written permission.
14 #
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL <COPYright HOLDER> BE LIABLE FOR ANY
19 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 import os
27 import subprocess
28 
29 def exportToEpsList(eps_file_list, latex_formulas):
30  in_read, in_write = os.pipe()
31  os.write(in_write, "\\documentclass{minimal}\n")
32  os.write(in_write, "\\usepackage{amsmath}\n")
33  os.write(in_write, "\\usepackage{mathtools}\n")
34  os.write(in_write, "\\usepackage{lmodern}\n")
35  os.write(in_write, "\\begin{document}\n")
36 
37  new_page = False
38  for f in latex_formulas:
39  if new_page:
40  os.write(in_write, "\\clearpage\n")
41  new_page = True
42  os.write(in_write, "\\begin{gather*}" + f + "\\end{gather*}\n")
43 
44  os.write(in_write, "\\end{document}\n")
45  os.close(in_write)
46 
47  # generate dvi file from latex document
48  subprocess.call(['latex', '-output-directory=/tmp'], stdin=in_read)
49  os.close(in_read)
50 
51  page_num = 1
52  for (handle, path) in eps_file_list:
53  subprocess.call(['dvips', '/tmp/texput.dvi', '-pp', str(page_num), '-o', '/tmp/texput.ps'])
54  subprocess.call(['ps2eps', '/tmp/texput.ps', '-f'])
55  with open('/tmp/texput.eps', 'r') as infile:
56  data = infile.read()
57  with open(path, 'w') as outfile:
58  outfile.write(data)
59  page_num += 1
60 
61  try:
62  os.remove('/tmp/texput.dvi')
63  except:
64  pass
65 
66  try:
67  os.remove('/tmp/texput.ps')
68  except:
69  pass
70 
71  try:
72  os.remove('/tmp/texput.eps')
73  except:
74  pass
75 
76 #def convertEpsListToPdf(eps_file_list):
77 # for (handle, path) in eps_file_list:
78 # head, tail = os.path.split(path)
79 # subprocess.call(['epspdf', path, tail+".pdf"])
80 
81 def toMathText(text):
82  return '\\text{' + text.replace('_', '\_') + '}'
83 
84 def removeEpsListFiles(eps_file_list):
85  for (handle, file_name) in eps_file_list:
86  os.close(handle)
87  os.remove(file_name)
88 
def removeEpsListFiles(eps_file_list)
def exportToEpsList(eps_file_list, latex_formulas)