Skip to content

Commit 89672cf

Browse files
Handle beamer overlay marks and report only the exact graphics file if given with an extension (#8)
Co-authored-by: Tobias Raabe <[email protected]>
1 parent bea8db8 commit 89672cf

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

CHANGES.rst

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ in reverse chronological order. Releases follow `semantic versioning
88
<https://anaconda.org/pytask/latex-dependency-scanner>`_.
99

1010

11+
0.0.4 - 2021-08-08
12+
------------------
13+
14+
- :gh:`8` changes ``scan`` such that only one path to an image file is returned if the
15+
image extension is given and file does not exist. Also, beamer overlays are ignored.
16+
Thanks to :ghuser:`brettviren`!
17+
18+
1119
0.0.3 - 2021-03-05
1220
------------------
1321

src/latex_dependency_scanner/scanner.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""Includes the ability to scan a LaTeX document."""
22
import re
3+
from os.path import splitext
34
from pathlib import Path
45
from typing import List
56
from typing import Optional
67
from typing import Union
78

8-
99
COMMON_TEX_EXTENSIONS = [".ltx", ".tex"]
1010
"""List[str]: List of typical file extensions that contain latex"""
1111

@@ -37,7 +37,9 @@
3737

3838
REGEX_TEX = re.compile(
3939
r"\\(?P<type>usepackage|RequirePackage|include|addbibresource|bibliography|putbib|"
40-
r"includegraphics|input|(sub)?import|lstinputlisting)(\[[^\[\]]*\])?"
40+
r"includegraphics|input|(sub)?import|lstinputlisting)"
41+
r"(<[^<>]*>)?"
42+
r"(\[[^\[\]]*\])?"
4143
r"({(?P<relative_to>[^{}]*)})?{(?P<file>[^{}]*)}",
4244
re.M,
4345
)
@@ -130,7 +132,11 @@ def yield_nodes_from_node(
130132
elif match.group("type") in ["input", "include", "import", "subimport"]:
131133
common_extensions = [".tex"]
132134
elif match.group("type") == "includegraphics":
133-
common_extensions = COMMON_GRAPHICS_EXTENSIONS
135+
ext = splitext(path)[-1]
136+
if ext in COMMON_GRAPHICS_EXTENSIONS:
137+
common_extensions = [ext]
138+
else:
139+
common_extensions = COMMON_GRAPHICS_EXTENSIONS
134140
elif match.group("type") == "lstinputlistings":
135141
common_extensions = [""]
136142
else:

tests/test_scan.py

+39-29
Original file line numberDiff line numberDiff line change
@@ -82,65 +82,75 @@ def test_input_or_include_without_extension_and_file(tmp_path, directive):
8282
@needs_latexmk
8383
@pytest.mark.end_to_end
8484
@pytest.mark.parametrize("image_ext", COMMON_GRAPHICS_EXTENSIONS)
85-
def test_includegraphics(tmp_path, image_ext):
86-
"""Test includegraphics.
85+
@pytest.mark.parametrize("has_extension", [True, False])
86+
@pytest.mark.parametrize("file_exists", [True, False])
87+
def test_includegraphics(tmp_path, image_ext, has_extension, file_exists):
88+
"""Test includegraphics with/out extensions and if (not) the image file exists.
8789
8890
Using post-script image files does not work with latexmk, since the .ps-file is
8991
converted to .pdf every run and latexmk will interpret it as a new file every time.
9092
Can be resolved by allowing pdflatex as the builder.
9193
9294
"""
93-
if image_ext == ".ps":
95+
if image_ext == ".ps" and file_exists:
9496
pytest.xfail(
9597
".ps does not work with latexmk: https://tex.stackexchange.com/a/67904."
9698
)
97-
if image_ext == ".eps":
99+
if image_ext == ".eps" and file_exists:
98100
pytest.xfail(
99101
".eps maybe needs \\graphicspath: https://tex.stackexchange.com/a/98886."
100102
)
101103

102-
source = """
103-
\\documentclass{article}
104-
\\usepackage{graphicx}
105-
% \\usepackage{auto-pst-pdf} necessary for .ps-files.
106-
\\begin{document}
107-
\\includegraphics{image}
108-
\\end{document}
104+
source = f"""
105+
\\documentclass{{article}}
106+
\\usepackage{{graphicx}}
107+
\\begin{{document}}
108+
\\includegraphics{{image{image_ext if has_extension else ''}}}
109+
\\end{{document}}
109110
"""
110111
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
111112

112113
# In case no extension is passed, we pick the pdf.
113114
_image_ext = image_ext if image_ext else ".pdf"
114-
shutil.copy(TEST_RESOURCES / f"image{_image_ext}", tmp_path / f"image{_image_ext}")
115115

116-
compile_pdf(tmp_path / "document.tex", tmp_path / "bld" / "document.pdf")
116+
if file_exists:
117+
shutil.copy(
118+
TEST_RESOURCES / f"image{_image_ext}", tmp_path / f"image{_image_ext}"
119+
)
120+
compile_pdf(tmp_path / "document.tex", tmp_path / "bld" / "document.pdf")
117121

118122
nodes = scan(tmp_path.joinpath("document.tex"))
119123

120-
assert nodes == [
121-
tmp_path.joinpath("document.tex"),
122-
tmp_path.joinpath(f"image{_image_ext}"),
123-
]
124+
expected = [tmp_path.joinpath("document.tex")]
125+
if has_extension or file_exists:
126+
expected.append(tmp_path.joinpath(f"image{_image_ext}"))
127+
else:
128+
expected.extend(
129+
[
130+
(tmp_path / "image").with_suffix(suffix)
131+
for suffix in COMMON_GRAPHICS_EXTENSIONS
132+
]
133+
)
134+
135+
assert nodes == expected
124136

125137

126138
@pytest.mark.end_to_end
127-
def test_includegraphics_without_extension_and_non_existent_file(tmp_path):
128-
source = """
129-
\\documentclass{article}
130-
\\usepackage{graphicx}
131-
\\begin{document}
132-
\\includegraphics{image}
133-
\\end{document}
139+
def test_includegraphics_with_beamer_overlay(tmp_path):
140+
source = r"""
141+
\documentclass{beamer}
142+
\usepackage{graphicx}
143+
\begin{document}
144+
\begin{frame}
145+
\includegraphics<1>{image.pdf}
146+
\end{frame}
147+
\end{document}
134148
"""
135149
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(source))
136150

137151
nodes = scan(tmp_path.joinpath("document.tex"))
138152

139-
expected = [tmp_path / "document.tex"] + [
140-
(tmp_path / "image").with_suffix(suffix)
141-
for suffix in COMMON_GRAPHICS_EXTENSIONS
142-
]
143-
assert nodes == expected
153+
assert nodes == [tmp_path.joinpath("document.tex"), tmp_path.joinpath("image.pdf")]
144154

145155

146156
@needs_latexmk

0 commit comments

Comments
 (0)