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
17 changes: 17 additions & 0 deletions sphinx/texinputs/sphinxlatexcontainers.sty
@@ -0,0 +1,17 @@
%% 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. User-defined
% formatting of directives can be achieved by renewing the definition of this
% environment, and branching on the value of the argument.
dham marked this conversation as resolved.
Show resolved Hide resolved

\ifx\sphinxclass\undefined % poor man's "provideenvironment"
dham marked this conversation as resolved.
Show resolved Hide resolved
\newenvironment{sphinxclass}[1]{
dham marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I wonder if #1 needs some sanitizing of any sort. Accented letters or other special chars are not usable as LaTeX environment names and could cause breakage. But digits and spaces will be ok.

\def\sphinxClassFunctionName{sphinxCLASS#1}%
\csname \sphinxClassFunctionName \endcsname}%
dham marked this conversation as resolved.
Show resolved Hide resolved
{\csname end\sphinxClassFunctionName \endcsname}%
dham marked this conversation as resolved.
Show resolved Hide resolved
\fi
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{sphinxclass}{%s}' % c)
dham marked this conversation as resolved.
Show resolved Hide resolved

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

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
dham marked this conversation as resolved.
Show resolved Hide resolved

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{sphinxclass}{classname}' in result
assert r'\end{sphinxclass}' in result