Highlighting Issue in Quarto's DOCX Output Format

Hello everyone,

I'm encountering an issue with Quarto when using any highlight style for code in DOCX format. Specifically, while the code itself is appropriately highlighted, the output is also unexpectedly highlighted in the same style. I want to restrict the highlighting only to the code, not it's output. This is not a problem in PDF or HTML formats, where only the code is highlighted as intended.

I've checked the documentation and explored various settings, but haven't found a solution yet.

Thank you in advance for any assistance you can provide!

Best regards

Steps to reproduce:
image


Actual behaviour:

Expected behaviour: (as in the HTML format)
I want just the code highlighted, as in the HTML/PDF formats, not the code output as well.
image

Environment:

RStudio: 2024.04.2 Build 764
R version: 4.4.1 (2024-06-14 ucrt)
OS: Windows 10 Pro

Quarto check:

Quarto 1.4.547
[>] Checking versions of quarto binary dependencies...
Pandoc version 3.1.11: OK
Dart Sass version 1.69.5: OK
Deno version 1.37.2: OK
[>] Checking versions of quarto dependencies......OK
[>] Checking Quarto installation......OK
Version: 1.4.547
Path: C:\Users\nickt\AppData\Local\Programs\Quarto\bin
CodePage: 1252

[>] Checking tools....................OK
TinyTeX: (external install)
Chromium: (not installed)

[>] Checking LaTeX....................OK
Using: TinyTex
Path: C:\Users\nickt\AppData\Roaming\TinyTeX\bin\windows
Version: 2023

[>] Checking basic markdown render....OK

[>] Checking Python 3 installation....OK
Version: 3.11.3
Path: C:/Users/nickt/AppData/Local/Programs/Python/Python311/python.exe
Jupyter: (None)

  Jupyter is not available in this Python installation.
  Install with py -m pip install jupyter

[>] Checking R installation...........OK
Version: 4.4.1
Path: C:/PROGRA~1/R/R-44~1.1
LibPaths:
- C:/Users/nickt/AppData/Local/R/win-library/4.4
- C:/Program Files/R/R-4.4.1/library
knitr: 1.47
rmarkdown: 2.27

[>] Checking Knitr engine render......OK

So this is currently the fastest way i know about.

  1. Go to you directory holding your qmd file in the terminal and run
quarto pandoc -o custom-reference-doc.docx \
   --print-default-data-file reference.docx

Word Templates – Quarto. This will create the custom-reference-doc.docx.

  1. open custom-reference-doc.docx in word and write some text and style it however you want it to be. Then select the text and go to styles and create a new style called Source Code

  2. Save the file

  3. Change your quarto document to

---
title: "187102"
format: 
  docx:
    reference-doc: custom-reference-doc.docx
---

## Test

```{r}
summary(iris)
```

output

I also changed the title header to be red for testing purposes.
Hope it helps.

1 Like

Thank you for your answer. That seems to work.

There's still a somehow awkward difference between the docx and html outputs. In the html output, using the nord highlight-style, Quarto creates a whole shaded box behind the code which covers the whole screen. In the docx output, it seems like it's only highlighting the source code text - the rest of the line remains unshaded (blank):

docx output:
image

html (desired) output:
image

Is there a fix for this?
Best regards

Seems like the styles for code are text and not paragraph based - for example

iris |>

contains

type or paste code here

<w:r>
	<w:rPr>
		<w:rStyle w:val="NormalTok"/>
	</w:rPr>
	<w:t xml:space="preserve">iris </w:t>
</w:r>
<w:r>
	<w:rPr>
		<w:rStyle w:val="SpecialCharTok"/>
	</w:rPr>
	<w:t xml:space="preserve">|&gt;</w:t>
</w:r>

two styles (NormalTok, SpecialCharTok) which are both text based.
Normally you would right click on Borders and shading and under Shading (in the menu that opens) switch from text to paragraph (this would give the desired effect) but it seems not possible here.

So we need someone who knows the internals of the generation of the styles in order to fix this properly.

1 Like

Thanks for your response. Really appreciated.

I believe this is a Pandoc behavior which does highlight everything a Source Code paragraph style. This is possibly because there is no difference in AST between both. Applying Custom Styles could help Pandoc - Pandoc User’s Guide it could even be automated for any cell output by using a Lua filter to tweak the AST. But this is quite advanced.

Modifying the reference document as you would like it to be seem the best way right now.

I can't reproduce with default styling

Maybe your custom reference doc gets in the way ?