sphinx
sphinx copied to clipboard
LaTeX: long list of authors overflows in margin of title page
To Reproduce Steps to reproduce the behavior:
Create using sphinx-quickstart a new project and respond with a long list of authors to the "author" prompt. Then do make latexpdf.
For example
> Nom du projet: A
> Nom(s) de l'auteur: John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team
> version du projet []: 1
Expected behavior
The list of authors display cleanly on title pagee
Observed behavior

Your project
Not mine, this is the https://github.com/matplotlib/matplotlib author string!
Environment info
- OS: [e.g. Unix/Linux/Mac/Win/other with version]
- Python version: 3.7.5
- Sphinx version: any
Relates https://github.com/matplotlib/matplotlib/issues/15796 in so far as the latter is about building their PDF docs...
A possible fix could be
diff --git a/sphinx/builders/latex/__init__.py b/sphinx/builders/latex/__init__.py
index c24e87a13..3f71be9b0 100644
--- a/sphinx/builders/latex/__init__.py
+++ b/sphinx/builders/latex/__init__.py
@@ -446,6 +446,7 @@ def default_latex_documents(config: Config) -> List[Tuple[str, str, str, str, st
""" Better default latex_documents settings. """
project = texescape.escape(config.project, config.latex_engine)
author = texescape.escape(config.author, config.latex_engine)
+ author = author.replace(', ', '\\and ').replace(' and ', '\\and and ')
return [(config.master_doc,
make_filename_from_project(config.project) + '.tex',
texescape.escape_abbr(project),
Not making a PR because some loose ends:
-
should we really fiddle with "and" in input text, and what then about internationalization?
-
perhaps the replacement of
,by\and<space>should be by<space>\and<space>, because of https://github.com/sphinx-doc/sphinx/blob/c235a43007f896acb89f27ab84bd16ccd95a3fba/sphinx/texinputs/sphinxmanual.cls#L56 for the PDF meta data which producesauthor1& author2& ...currently and would giveauthor1 & author2 &.... The extra space will not show on title page because the author name is in tabular cell. Or better to modify https://github.com/sphinx-doc/sphinx/blob/c235a43007f896acb89f27ab84bd16ccd95a3fba/sphinx/texinputs/sphinxmanual.cls#L56 and insert space before&there. Unfortunately I could not get understanding on how PDF specification is supposed to handle multiple authors meta data. -
maybe user inputs
author1,author2,...without spaces then the above will not succeed in inserting the LaTeX\and -
improve https://github.com/sphinx-doc/sphinx/blob/2.0/doc/usage/configuration.rst ? it only says
- author : The author name(s) of the document. The default value is 'unknown'.
without guideline of what to do in case of multiple authors (related to previous item)
-
better via a new function in
sphinx.util.texescape?
First, we need to add a guideline about author because there are no reason to handle "and" keyword especially.
For anybody encountering this, a quick and dirty workaround is to redefine \maketitle to insert a call to \author using the latex_elements configuration in your conf.py just before the title is rendered. You have to re-type the authors buuut you could automate this if you wanted. Example in conf.py:
...
author = "Author One, Author Two, Author Three, Author Four, Author Five, Author Six" # regular author list
# Fudge the latex author list, insert \and where you want the line break to appear
latex_elements={
"maketitle":
r"""\author{Author One, Author Two, Author Three, Author Four, \and Author Five, Author Six}
\sphinxmaketitle
"""
}
...
Thank you. More elaborate code splittin the author list if it is too long for the line.
# Fudge the latex author list, insert \and where you want the line break to appear
max_characters_author_line = 50
if len(author) > max_characters_author_line:
author_list = [author.strip() for author in author.split(',')]
i = len(author_list)
for i in range(1, len(author_list)):
if len(", ".join(author_list[:i])) > max_characters_author_line:
i = i-1
break
latex_author = ", ".join(author_list[:i]) + ", \\and " + ", ".join(author_list[i:])
else:
latex_author = author
latex_elements = {
"maketitle": "\\author{" + latex_author + "}\\sphinxmaketitle"
}