Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support containers in LaTeX output. #9166

Merged
merged 11 commits into from Jul 5, 2021
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