sphinx-doc / sphinx

The Sphinx documentation generator
https://www.sphinx-doc.org/
Other
6.51k stars 2.12k forks source link

LaTeX: long list of authors overflows in margin of title page #6875

Open jfbu opened 4 years ago

jfbu commented 4 years ago

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

Capture d’écran 2019-11-30 à 20 19 10

Your project

Not mine, this is the https://github.com/matplotlib/matplotlib author string!

Environment info

Relates https://github.com/matplotlib/matplotlib/issues/15796 in so far as the latter is about building their PDF docs...

jfbu commented 4 years ago

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:

tk0miya commented 4 years ago

First, we need to add a guideline about author because there are no reason to handle "and" keyword especially.

jasonmhite commented 7 months ago

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
"""
}
...
laclaro commented 6 months ago

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"
}