2

I'm using bookdown to prepare some documents. Below is the simple minimal reproducible example:

---
papersize: a6
site: bookdown::bookdown_site

output:
  bookdown::pdf_document2: 
    keep_tex: true

---

This paragraph has inline code (really it is SHA512 sum of some object) - `2a46edf77d12d5e4f71c0ffc5fa7e7ea3ae6d96667a3d39ba8658eb5de634ee48669e6bc366509e516ba7ecda6986c52ee8cab751660a789b6d55a1c8dc8296c`.

The code block is below:

```
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
```

other style with highlightning:

```html
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
```

The source code lines are too long.

rendering of above example

What should I add to preamble to allow code wrapping (breaking in multiple lines) like for example as shown below?

2a46edf77d12d5e4f71c0ffc5fa7e7ea3a
e6d96667a3d39ba8658eb5de634ee48669e  
6bc366509e516ba7ecda6986c52ee8cab75
1660a789b6d55a1c8dc8296c

and

<meta http-equiv="Content-Type"
content="text/html; 
charset=utf-8">

Update: adding

header-includes:
- \usepackage{listings} \let\verbatim\undefined \let\verbatimend\undefined \lstnewenvironment{verbatim}{\lstset{breaklines,basicstyle=\ttfamily}}{}

helps to solve problem with plain code, but I found that I'm using some code fragments with highlighting.

The intermediate LaTeX is here.

13
  • Same question: can you show the intermediate .tex file? Commented Sep 6, 2020 at 13:45
  • @samcarter_is_at_topanswers.xyz thanks, here it is . Commented Sep 6, 2020 at 15:36
  • For the first part you could try with \usepackage{seqsplit} in the header include and then \seqsplit{\ttfamily 2a46edf77d12d5e4f71c0ffc5fa7e7ea3ae6d96667a3d39ba8658eb5de634ee48669e6bc366509e516ba7ecda6986c52ee8cab751660a789b6d55a1c8dc8296c} in your document Commented Sep 6, 2020 at 15:42
  • For the second part it would be better to use a suitable environment like lstlistings from the listings package that supports linebreaks, but no idea how to convince markdown to do this. You could try \usepackage{listings} \let\verbatim\undefined \let\verbatimend\undefined \lstnewenvironment{verbatim}{\lstset{breaklines,basicstyle=\ttfamily}}{} but no guarantee that this won't spectacularly break some other things... Commented Sep 6, 2020 at 15:57
  • @samcarter_is_at_topanswers.xyz the first part is currently not universal. Do you have any ideas? The second is working. But I found that it does not work for blocks with highlighting (they use Shaded and Highlighting commands). Could you please check new LaTeX file and suggest solution for inline and Shaded blocks? Commented Sep 6, 2020 at 17:06

1 Answer 1

2

Note:

The following code snippets are just hacky workarounds, in particular the third one does not work in general and has the ability to break a great many things... instead of using them, please try convince the tool, that creates the tex code, to use suitable environments and macros.

To add line breaks to the Verbatim environment:

\usepackage{listings} 
\let\verbatim\undefined 
\let\verbatimend\undefined 
\lstnewenvironment{verbatim}{\lstset{breaklines,basicstyle=\ttfamily}}{}

To add line breaks to the Highlight environment:

\usepackage{fvextra} 
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{breaklines,commandchars=\\\{\}}

To add line breaks to your hash code

\usepackage{seqsplit}
\renewcommand{\texttt}[1]{{\ttfamily\seqsplit{#1}}}

(to say this again, I don't recommend redefining \texttt, that's really not a good idea, but the OP insisted...)

Sign up to request clarification or add additional context in comments.

1 Comment

I made an edit for verbatim environment as well. You are correct about the premise, but I'm really happy with the solution provided by these hacks. They are simpler than modifying bookdown→rmarkdown→knitr→pandoc chain by creating custom TeX-template or something more complex and difficult. All I need is to get adequate PDF file. The other possible solution is to use the pagedown, but it requires adaption of the HTML+CSS template which is time-consuming procedure. So your solution is absolutely what I need. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.