Skip to content

Commit

Permalink
Add pdf()
Browse files Browse the repository at this point in the history
  • Loading branch information
kwankyu committed Oct 4, 2024
1 parent b0f09d9 commit 841298f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
84 changes: 78 additions & 6 deletions src/sage/misc/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1683,7 +1683,9 @@ def _latex_file_(objects, title='SAGE', debug=False,
s = LATEX_HEADER + '\n' + MACROS + s + '\n\\end{document}'

if debug:
print('----')

Check warning on line 1686 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1686

Added line #L1686 was not covered by tests
print(s)
print('----')

Check warning on line 1688 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1688

Added line #L1688 was not covered by tests

return s

Expand Down Expand Up @@ -1827,7 +1829,6 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
...
ValueError: Unsupported LaTeX engine.
"""

if tightpage:
if margin is None:
margin_str = ""
Expand Down Expand Up @@ -1870,16 +1871,16 @@ def view(objects, title='Sage', debug=False, sep='', tiny=False,
tmp.cleanup()
return
output_file = os.path.join(tmp.name, "sage." + suffix)
# this should get changed if we switch the stuff in misc.viewer to
# producing lists

if debug:
print('viewer: "{}"'.format(viewer))
print(f'temporary file: "{output_file}"')
print(f'viewer: "{viewer}"')

Check warning on line 1877 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1876-L1877

Added lines #L1876 - L1877 were not covered by tests

# Return immediately but only clean up the temporary file after
# the viewer has closed. This function is synchronous and waits
# for the process to complete...
def run_viewer():
run([viewer, output_file], capture_output=True)
run([*viewer.split(), output_file], capture_output=True)

Check warning on line 1883 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1883

Added line #L1883 was not covered by tests
tmp.cleanup()

# ...but we execute it asynchronously so that view() completes
Expand All @@ -1890,7 +1891,78 @@ def run_viewer():
t.daemon = True
t.start()

return

def pdf(x, filename, tiny=False, tightpage=True, margin=None, engine=None, debug=False):
"""
Create an image from the latex representation of ``x`` and save it as a pdf
file with the given filename.
INPUT:
- ``x`` -- a Sage object
- ``filename`` -- the filename with which to save the image
- ``tiny`` -- boolean (default: ``False``); if ``True``, use a tiny font
- ``tightpage`` -- boolean (default: ``True``); use the LaTeX package
``preview`` with the 'tightpage' option
- ``margin`` -- float (default: no margin); width of border, only effective
with 'tight page'
- ``engine`` -- (default: ``None``) ``'latex'``, ``'pdflatex'``,
``'xelatex'`` or ``'lualatex'``; if ``None``, the value defined in the
LaTeX global preferences ``latex.engine()`` is used
- ``debug`` -- boolean (default: ``False``); if ``True``, print verbose output
EXAMPLES::
sage: # optional - latex
sage: from sage.misc.latex import pdf
sage: import tempfile
sage: with tempfile.NamedTemporaryFile(suffix=".pdf") as f: # random
....: pdf(ZZ[x], f.name)
"""
from sage.plot.graphics import Graphics
if isinstance(x, Graphics):
x.save(filename)
return

Check warning on line 1931 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1928-L1931

Added lines #L1928 - L1931 were not covered by tests

if tightpage:
if margin is None:
margin_str = ""

Check warning on line 1935 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1933-L1935

Added lines #L1933 - L1935 were not covered by tests
else:
margin_str = '\n\\setlength\\PreviewBorder{%fmm}' % margin
latex_options = {'extra_preamble':

Check warning on line 1938 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1937-L1938

Added lines #L1937 - L1938 were not covered by tests
'\\usepackage[tightpage,active]{preview}\n' +
'\\PreviewEnvironment{page}%s' % margin_str,
'math_left': '\\begin{page}$',
'math_right': '$\\end{page}'}
else:
latex_options = {}

Check warning on line 1944 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1944

Added line #L1944 was not covered by tests

# create a string of latex code to write in a file
s = _latex_file_([x], title='', tiny=tiny, debug=debug, **latex_options)
if engine is None:
engine = _Latex_prefs._option["engine"]

Check warning on line 1949 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1947-L1949

Added lines #L1947 - L1949 were not covered by tests
# path name for permanent pdf output
abs_path_to_pdf = os.path.abspath(filename)

Check warning on line 1951 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1951

Added line #L1951 was not covered by tests
# temporary directory to store stuff
with TemporaryDirectory() as tmp:
tex_file = os.path.join(tmp, "sage.tex")
pdf_file = os.path.join(tmp, "sage.pdf")

Check warning on line 1955 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1953-L1955

Added lines #L1953 - L1955 were not covered by tests
# write latex string to file
with open(tex_file, 'w') as file:
file.write(s)

Check warning on line 1958 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1957-L1958

Added lines #L1957 - L1958 were not covered by tests
# run latex on the file
e = _run_latex_(tex_file, debug=debug, engine=engine)
if e == 'pdf':

Check warning on line 1961 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1960-L1961

Added lines #L1960 - L1961 were not covered by tests
# if no errors, copy pdf_file to the appropriate place
shutil.copy(pdf_file, abs_path_to_pdf)

Check warning on line 1963 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1963

Added line #L1963 was not covered by tests
else:
print("Latex error or no pdf was generated.")

Check warning on line 1965 in src/sage/misc/latex.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/latex.py#L1965

Added line #L1965 was not covered by tests


def png(x, filename, density=150, debug=False,
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def default_viewer(viewer=None):
elif os.uname()[0] == 'Darwin':
# Simple on OS X, since there is an open command that opens
# anything, using the user's preferences.
BROWSER = 'open'
BROWSER = 'open -W'

Check warning on line 72 in src/sage/misc/viewer.py

View check run for this annotation

Codecov / codecov/patch

src/sage/misc/viewer.py#L72

Added line #L72 was not covered by tests
DVI_VIEWER = BROWSER
PDF_VIEWER = BROWSER
PNG_VIEWER = BROWSER
Expand Down

0 comments on commit 841298f

Please sign in to comment.