Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm having an issue with the Sphinx Documentation Generator, you may be asking what this has to do with LaTeX. Well, we were hoping to use it for our specification documentation which needs to be generated as a PDF. Sphinx generates PDF output by first converting reStructuredText to LaTeX, then LaTeX to PDF.

In our documentation there exists some(okay, a lot) of very long class/interface/etc names such as

some.really.long.class.path.that.causes.our.spacing.to.be.atrocious

Often, these are referenced mid paragraph (and sometimes many in one paragraph) and can cause issues like pictured below:

image

And here is a sample of the LaTeX that is generated by sphinx that was used to create the above screenshot of the issue:

% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt,english]{sphinxmanual}
\usepackage[utf8]{inputenc}
\DeclareUnicodeCharacter{00A0}{\nobreakspace}
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{times}
\usepackage[Bjarne]{fncychap}
\usepackage{longtable}
\usepackage{sphinx}
\usepackage{multirow}


\title{spacingexample Documentation}
\date{April 01, 2013}
\release{1}
\author{me}
\newcommand{\sphinxlogo}{}
\renewcommand{\releasename}{Release}
\makeindex

\makeatletter
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax%
    \let\PYG@ul=\relax \let\PYG@tc=\relax%
    \let\PYG@bc=\relax \let\PYG@ff=\relax}
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname}
\def\PYG@toks#1+{\ifx\relax#1\empty\else%
    \PYG@tok{#1}\expandafter\PYG@toks\fi}
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{%
    \PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}}
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}}

\expandafter\def\csname PYG@tok@gd\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\expandafter\def\csname PYG@tok@gu\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\expandafter\def\csname PYG@tok@gt\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.25,0.82}{##1}}}
\expandafter\def\csname PYG@tok@gs\endcsname{\let\PYG@bf=\textbf}
\expandafter\def\csname PYG@tok@gr\endcsname{\def\PYG@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\expandafter\def\csname PYG@tok@cm\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}}
\expandafter\def\csname PYG@tok@vg\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@m\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@mh\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@cs\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\colorbox[rgb]{1.00,0.94,0.94}{\strut ##1}}}
\expandafter\def\csname PYG@tok@ge\endcsname{\let\PYG@it=\textit}
\expandafter\def\csname PYG@tok@vc\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@il\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@go\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.19,0.19,0.19}{##1}}}
\expandafter\def\csname PYG@tok@cp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@gi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\expandafter\def\csname PYG@tok@gh\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PYG@tok@ni\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.84,0.33,0.22}{##1}}}
\expandafter\def\csname PYG@tok@nl\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.13,0.44}{##1}}}
\expandafter\def\csname PYG@tok@nn\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.05,0.52,0.71}{##1}}}
\expandafter\def\csname PYG@tok@no\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.38,0.68,0.84}{##1}}}
\expandafter\def\csname PYG@tok@na\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@nb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@nc\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.05,0.52,0.71}{##1}}}
\expandafter\def\csname PYG@tok@nd\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.33,0.33,0.33}{##1}}}
\expandafter\def\csname PYG@tok@ne\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@nf\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.02,0.16,0.49}{##1}}}
\expandafter\def\csname PYG@tok@si\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.44,0.63,0.82}{##1}}}
\expandafter\def\csname PYG@tok@s2\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@vi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@nt\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.02,0.16,0.45}{##1}}}
\expandafter\def\csname PYG@tok@nv\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@s1\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@gp\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.78,0.36,0.04}{##1}}}
\expandafter\def\csname PYG@tok@sh\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@ow\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@sx\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.78,0.36,0.04}{##1}}}
\expandafter\def\csname PYG@tok@bp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@c1\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}}
\expandafter\def\csname PYG@tok@kc\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@c\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}}
\expandafter\def\csname PYG@tok@mf\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@err\endcsname{\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}
\expandafter\def\csname PYG@tok@kd\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@ss\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.32,0.47,0.09}{##1}}}
\expandafter\def\csname PYG@tok@sr\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.14,0.33,0.53}{##1}}}
\expandafter\def\csname PYG@tok@mo\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@mi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@kn\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@o\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PYG@tok@kr\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@s\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@kp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@w\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\expandafter\def\csname PYG@tok@kt\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.56,0.13,0.00}{##1}}}
\expandafter\def\csname PYG@tok@sc\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@sb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@k\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@se\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@sd\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}

\def\PYGZbs{\char`\\}
\def\PYGZus{\char`\_}
\def\PYGZob{\char`\{}
\def\PYGZcb{\char`\}}
\def\PYGZca{\char`\^}
\def\PYGZam{\char`\&}
\def\PYGZlt{\char`\<}
\def\PYGZgt{\char`\>}
\def\PYGZsh{\char`\#}
\def\PYGZpc{\char`\%}
\def\PYGZdl{\char`\$}
\def\PYGZti{\char`\~}
% for compatibility with earlier versions
\def\PYGZat{@}
\def\PYGZlb{[}
\def\PYGZrb{]}
\makeatother

\begin{document}

\maketitle
\tableofcontents
\phantomsection\label{index::doc}


Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer tristique, leo in dictum fermentum, lorem sapien rhoncus tortor, 
in fringilla sem magna in velit. Pellentesque bibendum ultricies ligula at bibendum. Proin gravida metus sit amet leo pretium fringilla 
tincidunt orci sollicitudin. Quisque suscipit ligula id massa ultricies eu ultricies lacus imperdiet. Aenean sagittis pharetra neque, 
sit amet aliquet turpis varius ut. Aliquam sed mauris sed risus volutpat bibendum. Praesent id luctus sapien. Class aptent taciti sociosqu 
ad litora torquent per conubia nostra, per inceptos himenaeos. Ut id orci risus. Curabitur vel rhoncus sapien. Aliquam sollicitudin semper dictum.
 In magna orci, tristique a suscipit ut, fringilla eget dolor. Pellentesque vel neque elit. Mauris in ipsum nibh.
 \code{super.long.class.name.that.we.are.talking.about.that.ruins.our.interword.spacing} Nunc nec mauris elit, sit amet ullamcorper augue. 
 Sed ornare est vel nibh venenatis eu faucibus lorem mattis. Nam lorem nulla, aliquam a convallis vel, lacinia sit amet lorem. Donec vulputate 
 neque in enim aliquam quis ullamcorper nunc consequat. Nunc at ipsum accumsan metus vehicula porta. another. \code{super.long.class.name.that.we.are.talking.about.} 
 Vivamus blandit  \code{more.really.long.class.stuff.} orci ac felis aliquam pharetra sollicitudin felis tincidunt.
 Pellentesque molestie varius dui vitae varius. Sed mauris dui, tristique in blandit tincidunt, iaculis sit amet augue. Curabitur ut tortor sapien,
 a tristique ligula. Etiam porta auctor purus, pretium posuere dolor rutrum ut. Phasellus velit nisl, gravida id volutpat at, porta at orci.
 Etiam dapibus augue sit amet ligula tristique accumsan. Vestibulum nibh elit, pharetra in aliquam ut, auctor id felis. Curabitur posuere euismod orci nec consectetur. 
 Curabitur vulputate, sem eu auctor scelerisque, neque metus aliquet neque, ultrices hendrerit lacus tellus et metus.

Contents:


\chapter{Indices and tables}
\label{index:welcome-to-spacingexample-s-documentation}\label{index:indices-and-tables}\begin{itemize}
\item {} 
\emph{genindex}

\item {} 
\emph{modindex}

\item {} 
\emph{search}

\end{itemize}



\renewcommand{\indexname}{Index}
\printindex
\end{document}

Now the reason I'm asking on this StackExchange instead of one related to reStructuredText is because I'm hoping there is some simple LaTeX solution. Sphinx supplies it's user with a LaTeX preamble variable that allows me to inject LaTeX into the beginning of the intermediate .tex file that is used to generate the PDF.

So, does anyone have some sort of solution to my issue? I've seen suggestions where you can give LaTeX "hints" on where it is acceptable to insert hyphens into these really long code tags but there is no built in way for me to get Sphinx to be able to pass through that type of syntax that I know of.

What I need is the class paths to be the teletype/monospaced font style, but have them look better than the default behavior in regards to the spacing between words. Is there some way to redefine the code tags to accomplish something like this?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

enter image description here

I didn't have the sphinx files so used report class and ignored the errors but copying the idea of the url package you can use math mode and then allow breaking on . (or other characters)

\def\code#1{$%
\medmuskip0mu \mathcode`\.="213A 
\mathtt{\xdef\tmp{\the\fam}}\fam\tmp#1$}

in chat egreg suggested this improved version but I don't think it will address the pythong error that you mention in comments (I have no idea about that)

\makeatletter

\newcommand{\code}[1]{$
  \medmuskip=0mu
  \mathcode`\.="2\hexnumber@{\tt@mathgroup\space}2E % period
  \mathcode`\-="2\hexnumber@{\tt@mathgroup\space}2D % hyphen
  \mathgroup\tt@mathgroup#1$}

\sbox0{$\mathtt{\xdef\tt@mathgroup{\the\fam}}$}
\makeatother

It appears form comments that the monospace font used in text is not the same as used in math in the sphinx setup so this locally makes sure the text font is used

\def\code#1{\texttt{%
\medmuskip0mu
\mathcode`\.="202E
\mathcode`\-="202D
$\fam0 \textfont0\font
#1$}}

defines \code to use tt font on math mode with . set as a binary operator with the medium spacing normally applied set to 0 (You can make this a small stretch space to allow line breaking such as 0mu plus 2mu if you don't mind space appearing around the ..

share|improve this answer
    
This is causing a build error when trying to add it to my preamble variables, Exception occurred: File "/usr/local/lib/python2.7/dist-packages/Sphinx-1.2b1dev_20130401-py2.7.egg/sphin‌​x/writers/latex.py", line 284, in astext self.generate_indices() + UnicodeDecodeError: 'ascii' codec can't decode byte 0xde in position 414: ordinal not in range(128) –  John Lotacs Apr 1 '13 at 20:30
    
@JohnLotacs ah well you probably need to keep that definition on the latex side of things, I don't know this system at all but why does adding a latex definition affect what's passed to python? and where did byte 0xde come from? –  David Carlisle Apr 1 '13 at 21:13
    
I think my problem was I had to escape every backslash. However, it isn't building for me, I get ! LaTeX Error: Command \code already defined. Or name \end... illegal, see p.192 of the manual. I inspected the intermediate .tex file and ergreg's solution was inserted perfectly after the \usepackage definitions. –  John Lotacs Apr 1 '13 at 22:26
    
Your solution compiles and fixes spacing, but the font styling is a little too subtle. The original seems to have a thinner font, I'm unfamiliar with latex, can you tweak it a bit? –  John Lotacs Apr 1 '13 at 22:33
    
@JohnLotacs @egreg and I were using the standard classes where \code isn't defined so \newcommand you need to redefifine the existing command from the sphinx setup so use \renewcommand. If you could show the orignal code definition we could probably see what font it is using and use the same one –  David Carlisle Apr 1 '13 at 22:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.