Skip to content

Commit

Permalink
Define \sphinxliteraltext and \sphinxcodestyle for flexibility
Browse files Browse the repository at this point in the history
This addresses the \sphinxcode usage issue in context of styling of
declarations, as discussed in parent commit message.
  • Loading branch information
jfbu committed Jan 2, 2018
1 parent cc632ba commit 88cf9be
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions sphinx/texinputs/sphinx.sty
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@
\@namedef{PYG@tok@k-cpp}{%
\def\PYG@tc##1{\textcolor[RGB]{0,0,"FF}{##1}}%
}%
\@namedef{PYG@tok@d}{% default desc style uses \sphinxcodestyle (i.e. \texttt)
\def\PYG@ff##1{\sphinxcodestyle{##1}}%
}%

%% OPTIONS
%
Expand Down Expand Up @@ -1443,13 +1446,13 @@
%
\newlength{\py@argswidth}
\newcommand{\py@sigparams}[2]{%
\parbox[t]{\py@argswidth}{#1\sphinxcode{)}#2}}
\parbox[t]{\py@argswidth}{#1\sphinxcodestyle{)}#2}}
\newcommand{\pysigline}[1]{\item[{#1}]}
\newcommand{\pysiglinewithargsret}[3]{%
\settowidth{\py@argswidth}{#1\sphinxcode{(}}%
\settowidth{\py@argswidth}{#1\sphinxcodestyle{(}}%
\addtolength{\py@argswidth}{-2\py@argswidth}%
\addtolength{\py@argswidth}{\linewidth}%
\item[{#1\sphinxcode{(}\py@sigparams{#2}{#3}}]}
\item[{#1\sphinxcodestyle{(}\py@sigparams{#2}{#3}}]}
\newcommand{\pysigstartmultiline}{%
\def\pysigstartmultiline{\vskip\smallskipamount\parskip\z@skip\itemsep\z@skip}%
\edef\pysigstopmultiline
Expand Down Expand Up @@ -1551,7 +1554,7 @@
% breakable at non-escaped . , ; ? ! / using \sphinxbreaksviaactive.
% the macro must be protected if it ends up used in moving arguments,
% in 'alltt' \@noligs is done already, and the \scantokens must be avoided.
\protected\def\sphinxcode#1{{\def\@tempa{alltt}%
\protected\def\sphinxliteraltext#1{{\def\@tempa{alltt}%
\ifx\@tempa\@currenvir\else
\ifspx@opt@inlineliteralwraps
\sphinxbreaksviaactive\let\sphinxafterbreak\empty
Expand All @@ -1562,12 +1565,14 @@
\let\do@noligs\sphinx@do@noligs
\@noligs\endlinechar\m@ne\everyeof{}% (<- in case inside \sphinxhref)
\expandafter\scantokens
\fi {\texttt{#1}}}}
\fi {#1}}}
\def\sphinx@do@noligs #1{\catcode`#1\active\begingroup\lccode`\~`#1\relax
\lowercase{\endgroup\def~{\leavevmode\kern\z@\char`#1 }}}
\def\sphinx@literal@nolig@list {\do\`\do\<\do\>\do\'\do\-}%
\protected\def\sphinxbfcode#1{\sphinxcode{\bfseries{}#1}}
\protected\def\sphinxcodestyle#1{\texttt{#1}}
\protected\def\sphinxcode#1{\sphinxcodestyle{\sphinxliteraltext{#1}}}

This comment has been minimized.

Copy link
@jfbu

jfbu Jan 2, 2018

Author Owner

The reason for not using \texttt{\sphinxliteraltext{#1}} here but \sphinxcodestyle is twofold:

  • usage in \pysigline. It is wasteful to issue \sphinxcode{(}, and it is arguably not good style to hard-code \texttt{(} in \pysigline. Hence the \sphinxcodestyle. This is not very strong point.

  • more important and decisive, the usage in definition of \PYG@tok@d in connection with latex.py visit_desc_element.

Here is what I would prefer to do: define \sphinxcode to do \texttt{#1} by default, but let the latex writer everywhere it uses currently \sphinxcode{%s} to issue \sphinxcode{\sphinxliteraltext{%s}}. Same for \sphinxbfcode.

Then we don't have a \sphinxcodestyle at all. I think I will make a PR on master for this discussion, because it is useful to extract from current \sphinxcode all the complicated "verbatim" set-up into a "\sphinxliteraltext" (name to be decided) macro and revert \sphinxcode to simple \texttt{#1} as in Sphinx 1.4 or earlier.

\protected\def\sphinxbfcode#1{\textbf{\sphinxcode{#1}}}
\protected\def\sphinxemail#1{\textsf{#1}}
\protected\def\sphinxtablecontinued#1{\textsf{#1}}
\protected\def\sphinxtitleref#1{\emph{#1}}
Expand Down
2 changes: 1 addition & 1 deletion sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,7 +1216,7 @@ def depart_desc_signature_line(self, node):

def visit_desc_element(self, node):
# type: (nodes.Node) -> None
self.body.append('\\PYG{%s}{\\sphinxcode{' % '+'.join(node['classes']))
self.body.append('\\PYG{%s}{\\sphinxliteraltext{' % '+'.join(node['classes']))
self.literal_whitespace += 1

def depart_desc_element(self, node):
Expand Down

0 comments on commit 88cf9be

Please sign in to comment.