Code Block Style produces incorrect LaTeX output

Environment: latest version of Scrivener on Windows 10 Pro

I’ve created a test project using the Non-Fiction LaTeX template. I then selected a group of text( LaTeX commands} and assigned the “code block” style to it. I compiled it to “tex” format and then compiled the resultant tex file with XeLaTeX and received several compile errors. Looking at the “tex” file Scrivener produced I see that it placed “/end{verbatim}” commands after every line in the code block.

Example:

\begin{verbatim}
\documentclass{article}
\end{verbatim}
\usepackage{times}
\end{verbatim}

\begin{document}
\end{verbatim}
\end{verbatim}

I know it’s easy to edit after the fact, but I’d rather not have to go through a book-sized file and correct all the errors in the tex file. Has anyone else encountered this, and is there a fix for this?

Hi marsvelasquez,

Welcome!

FYI, if you don’t find your answers here, a good source for answers to LaTeX issues can be found on StackExchange.

HTH,
scrive

This is a known bug, so I’m moving it over to the Windows area.

The problem is that Para+Char styles (as defined in the compile format design window) for some reason spam the range suffix over and over, on each line. Try creating the style again with “Paragraph Style” settings, and you should get better output.

Thanks for the reply. As part of my debugging process I did create a new paragraph style, and I got better, but still in correct output, This time it would output as follows:

\begin{verbatim}\documentclass{article}
\usepackage{times}
\begin{document}\end{verbatim}

I thought, perhaps, because each line ended with a paragraph, that that was the problem and used newline instead. That, unfortunately caused the compiler to output “\” characters at the end of each line.

I did, in the end, find that if I placed an extra paragraph character before and after the code block and selected those extra paragraph characters along with my code block and then assigned the style, that that would work.

Thanks again.
Mar

Sorry, I should have pointed out specifically that those fields have invisible whitespace characters in them.

That, unfortunately caused the compiler to output “\” characters at the end of each line.

There is a Replacement for that in the Format settings. As the word processing concept of a line break is foreign to plain-text files, I think if anything that would only cause problems in the .tex file anyway (or at best just do nothing), so it is translated to the closest analogue in syntax: \\.

I did, in the end, find that if I placed an extra paragraph character before and after the code block and selected those extra paragraph characters along with my code block and then assigned the style, that that would work.

That’s one way to go about it, but as you may see with what I’ve done in the original style (which unfortunately uses the broken para+char type and thus triggers a bug) is that the prefix and suffix fields actually have the newlines within the field—thus you don’t have to type them in yourself in the editor. The prefix and suffix system is designed for flexibility, which means it’s not going to assume that they should fall outside of the paragraphs it was assigned to. If you need that to happen, you must tell it that is what you want.

Unfortunately these fields do not show whitespace characters, nor do they allow for the input of them. What I do is copy and paste them into a text editor, modify them, and then copy and paste them back into the software. For the prefix, you would want:

\begin{verbatim}

And the suffix:


\end{verbatim}

Or if we could see the whitespace character in symbolic form:

\begin{verbatim}¶
¶\end{verbatim}

Thank you for the feedback. I had tried to do that(insert a paragraph char after the \begin{verbatim} and one before the \end{verbatim}} but couldn’t get it to accept it.

I hadn’t thought of doing it in a text editor first, then copy/pasting it into the styles prefix/suffix field.

Thanks again
Mar

The copy/paste from a text editor did it. Created another new style called “Code Listing” which uses the listings package and the \begin{lstlisting} and \end{lstlisting} prefix/sufix and that works.