-
Notifications
You must be signed in to change notification settings - Fork 50
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
code syntax highlighting does not work in LaTeX #8
Comments
Hmm, I'm not sure I ever tested that. It looks like those are pygments styles that are supposed to be written by sphinx, but I must have disabled them at some point. Looking quickly at the sphinx source, I'm not sure how they actually get written into the document. Are you up for looking into it? |
I have started looking into it. It is likely something in 'sphinx.sty' that has been commented out. I should put out the disclaimer that I am not a proper software dev. But would definitely like to make this work. MR |
For reference, here's the original version of the file: I wasn't sure where those pygments styles are coming from in there |
I have found a fix for the issue. But it introduces another (but lesser) issue -- which is aesthetic and far less crucial. def depart_literal_block(self, node):
code = self.verbatim.rstrip('\n')
lang = self.hlsettingstack[-1][0]
linenos = code.count('\n') >= self.hlsettingstack[-1][1] - 1
highlight_args = node.get('highlight_args', {})
if 'language' in node:
# code-block directives
lang = node['language']
highlight_args['force'] = True
if 'linenos' in node:
linenos = node['linenos']
def warner(msg):
self.builder.warn(msg, (self.curfilestack[-1], node.line))
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
linenos=linenos, **highlight_args)
# hlcode = hlcode.replace('\$', '$')
# hlcode = hlcode.replace('\%', '%')
# workaround for Unicode issue
hlcode = hlcode.replace(u'€', u'@texteuro[]')
# must use original Verbatim environment and "tabular" environment
if self.table:
hlcode = hlcode.replace('\\begin{Verbatim}',
'\\begin{OriginalVerbatim}')
self.table.has_problematic = True
self.table.has_verbatim = True
# get consistent trailer
hlcode = hlcode.rstrip()[:-14] # strip \end{Verbatim}
hlcode = hlcode.rstrip() + '\n'
# hlcode = '\n' + hlcode + '\\end{%sVerbatim}\n' % (self.table and 'Original' or '')
# hlcode = hlcode.replace('Verbatim', 'lstlisting')
# begin_bracket = hlcode.find('[')
# end_bracket = hlcode.find(']')
# hlcode = hlcode[:begin_bracket] + '[]' + hlcode[end_bracket+1:]
# self.body.append(hlcode)
# self.verbatim = None
self.body.append('\n' + hlcode + '\\end{%sVerbatim}\n' %
(self.table and 'Original' or ''))
self.verbatim = None |
could you submit it as a pull request and add a code block to the sample? |
I can do that. By 'sample' do mean the sample thesis? |
Yep, sorry I just meant the sample thesis. I was thinking |
I tried changing |
I don't like the idea of using Until now I still don't have a solution to the |
I think I uncommented all interesting parts in |
I am currently working on proving or disproving the assumption, that the wrong environment is used. Reason for this is, that the Pygments documentation says, that it needs the fancyvrb package and generates it output in a edit: Commenting out the edit: got it working. create pull request now. |
Highlighted Sourcecode from Pygments requires the fancyvrb and color packages. To get it working with the Sphinx LaTeX Parser some commands where necessary which were commented out. And last but not least creating a lstlisting environment is not a reasonable choice, considering that this package has the same job as Pygments. Therefore the overwritten method depart_literal_block was removed, that Sphinx' default will be used. The result is for some reasons rather ugly, but at least it works now. I also changed the code example to a Python function to show that highlighting really works now. FYI: For some reason I also needed to uncomment the `notice` environment defintion. Building from master without the changes introduced here didn't work at all. Might be something I did, though. But maybe this is a hint that a lot more of the sphinx.sty needs to be uncommented, but nobody noticed because it wasn't used until now. Fixes jterrace#8 Signed-off-by: Erik Bernoth <[email protected]>
I tried @erikb85's changed (hand merged, might have made some typos) and they seem to work for me. To get grey boxes I used:
I discovered the problem when I found underline characters being escaped in my code blocks; python code was also broken. @erikb85, can you submit a pull request? |
Hi,
Thank you very much for the well thought out and very useful code.
I have python code examples within my document. The html renders the syntax highlighting perfectly, but when I build latex (and from that pdf), the code comes up all jumbled up.
As a simple example, the following rest code...
comes out like this in the pdf:
Can you please advise if I am doing something wrong, or which files I should be looking into to fix this?
Thanks,
MR
The text was updated successfully, but these errors were encountered: