N0rbert
N0rbert

Reputation: 579

How to get text-wrapping for source code elements while using rmarkdown/bookdown?

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.

Upvotes: 2

Views: 1385

Answers (1)

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...)

Upvotes: 2

Related Questions