Skip to content

Commit

Permalink
Merge pull request #9166 from dham/container-latex
Browse files Browse the repository at this point in the history
Support containers in LaTeX output.
  • Loading branch information
jfbu committed Jul 5, 2021
2 parents 86c238d + 5b3bd3f commit f2c5c63
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions sphinx/texinputs/sphinx.sty
Expand Up @@ -270,6 +270,11 @@
\input{sphinxlatexshadowbox.sty}


%% CONTAINERS
%
\input{sphinxlatexcontainers.sty}


%% PYGMENTS
% stylesheet for highlighting with pygments
\RequirePackage{sphinxhighlight}
Expand Down
22 changes: 22 additions & 0 deletions sphinx/texinputs/sphinxlatexcontainers.sty
@@ -0,0 +1,22 @@
%% CONTAINER DIRECTIVES
%
% change this info string if making any custom modification
\ProvidesFile{sphinxlatexcontainers.sty}[2021/05/03 containers]

% The purpose of this file is to provide a dummy environment sphinxclass which
% will be inserted for each class in each container directive. The class name
% will be passed as the argument to the environment.
%
% For a class foo, the user can define customised handling of that class by
% defining the sphinxclassfoo LaTeX environment.

\newenvironment{sphinxuseclass}[1]{%
\def\sphinxClassFunctionName{sphinxclass#1}%
\ltx@ifundefined{\sphinxClassFunctionName}%
{}% undefined so do nothing
{\expandafter\begin\expandafter{\sphinxClassFunctionName}}%
}{%
\ltx@ifundefined{\sphinxClassFunctionName}%
{}% we did nothing so we keep doing nothing
{\expandafter\end\expandafter{\sphinxClassFunctionName}}%
}%
8 changes: 6 additions & 2 deletions sphinx/writers/latex.py
Expand Up @@ -1975,10 +1975,14 @@ def depart_compound(self, node: Element) -> None:
pass

def visit_container(self, node: Element) -> None:
pass
classes = node.get('classes', [])
for c in classes:
self.body.append('\n\\begin{sphinxuseclass}{%s}' % c)

def depart_container(self, node: Element) -> None:
pass
classes = node.get('classes', [])
for c in classes:
self.body.append('\n\\end{sphinxuseclass}')

def visit_decoration(self, node: Element) -> None:
pass
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions tests/roots/test-latex-container/index.rst
@@ -0,0 +1,4 @@
.. container:: classname

text

8 changes: 8 additions & 0 deletions tests/test_build_latex.py
Expand Up @@ -1599,3 +1599,11 @@ def test_latex_elements_extrapackages(app, status, warning):
def test_latex_nested_tables(app, status, warning):
app.builder.build_all()
assert '' == warning.getvalue()


@pytest.mark.sphinx('latex', testroot='latex-container')
def test_latex_container(app, status, warning):
app.builder.build_all()
result = (app.outdir / 'python.tex').read_text()
assert r'\begin{sphinxuseclass}{classname}' in result
assert r'\end{sphinxuseclass}' in result

0 comments on commit f2c5c63

Please sign in to comment.