Muhsabrys commited on
Commit
c79fbb9
·
verified ·
1 Parent(s): 1aacb51

Update latex-server.py

Browse files
Files changed (1) hide show
  1. latex-server.py +19 -22
latex-server.py CHANGED
@@ -141,20 +141,21 @@ def compile_latex():
141
  'error': str(exc)
142
  }), 400
143
 
144
- # Compile LaTeX to PDF using pdflatex
145
  try:
146
- command = ['pdflatex', '-interaction=nonstopmode', '-halt-on-error', '-output-directory', tmpdir, tex_file]
147
- result = None
148
- for _ in range(2):
149
- result = subprocess.run(
150
- command,
151
- capture_output=True,
152
- text=True,
153
- timeout=30,
154
- cwd=tmpdir,
155
- )
156
- if result.returncode != 0:
157
- break
 
158
 
159
  # Check if PDF was created
160
  if result.returncode != 0 or not os.path.exists(pdf_file):
@@ -177,7 +178,7 @@ def compile_latex():
177
  except FileNotFoundError:
178
  return jsonify({
179
  'success': False,
180
- 'error': 'pdflatex not found. Please install TeX Live: brew install texlive (macOS) or apt-get install texlive (Linux)'
181
  }), 500
182
 
183
  except Exception as e:
@@ -204,19 +205,15 @@ def index():
204
  })
205
 
206
  if __name__ == '__main__':
207
- # Check if pdflatex is available
208
  try:
209
- subprocess.run(['pdflatex', '--version'], capture_output=True, check=True)
210
  except (FileNotFoundError, subprocess.CalledProcessError):
211
- print('ERROR: pdflatex not found!')
212
- print('Please install TeX Live:')
213
- print(' macOS: brew install texlive')
214
- print(' Linux: sudo apt-get install texlive-latex-base texlive-latex-extra')
215
- print(' Windows: Download from https://tug.org/texlive/')
216
  sys.exit(1)
217
 
218
  print('🚀 LaTeX Compiler Server starting on http://localhost:5000')
219
  print('📝 Open latex-editor.html in your browser')
220
  print('Press Ctrl+C to stop')
221
 
222
- app.run(debug=False, host='0.0.0.0', port=5000)
 
141
  'error': str(exc)
142
  }), 400
143
 
144
+ # Compile LaTeX to PDF using xelatex + bibtex
145
  try:
146
+ command = ['xelatex', '-interaction=nonstopmode', '-halt-on-error', '-output-directory', tmpdir, tex_file]
147
+
148
+ # Pass 1: Initial xelatex run
149
+ subprocess.run(command, capture_output=True, text=True, timeout=30, cwd=tmpdir)
150
+
151
+ # Pass 2: Run bibtex for citations (like apacite)
152
+ subprocess.run(['bibtex', 'document'], capture_output=True, text=True, timeout=30, cwd=tmpdir)
153
+
154
+ # Pass 3: Second xelatex run (links citations)
155
+ subprocess.run(command, capture_output=True, text=True, timeout=30, cwd=tmpdir)
156
+
157
+ # Pass 4: Final xelatex run (resolves cross-references)
158
+ result = subprocess.run(command, capture_output=True, text=True, timeout=30, cwd=tmpdir)
159
 
160
  # Check if PDF was created
161
  if result.returncode != 0 or not os.path.exists(pdf_file):
 
178
  except FileNotFoundError:
179
  return jsonify({
180
  'success': False,
181
+ 'error': 'xelatex not found. Please check your TeX Live installation.'
182
  }), 500
183
 
184
  except Exception as e:
 
205
  })
206
 
207
  if __name__ == '__main__':
208
+ # Check if xelatex is available
209
  try:
210
+ subprocess.run(['xelatex', '--version'], capture_output=True, check=True)
211
  except (FileNotFoundError, subprocess.CalledProcessError):
212
+ print('ERROR: xelatex not found!')
 
 
 
 
213
  sys.exit(1)
214
 
215
  print('🚀 LaTeX Compiler Server starting on http://localhost:5000')
216
  print('📝 Open latex-editor.html in your browser')
217
  print('Press Ctrl+C to stop')
218
 
219
+ app.run(debug=False, host='0.0.0.0', port=7860)