diff --git a/CHANGES.rst b/CHANGES.rst
index 4e58b3e..99207fd 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,3 +1,8 @@
+Unreleased
+----------
+
+- Preserve tabs and line breaks between the characters of an escaped delimiter in Word templates.
+
0.21.0 (2026-11-15)
-------------------
First release as Docform. Rendering output matches docxtpl 0.20.2.
diff --git a/docform/docx/template.py b/docform/docx/template.py
index c3ed094..3e19f91 100644
--- a/docform/docx/template.py
+++ b/docform/docx/template.py
@@ -87,11 +87,10 @@ def patch_xml(self, src_xml):
strip all unnecessary xml tags, manage table cell background color and colspan,
unescape html entities, etc..."""
- # Join escaped Jinja delimiters that Word has split across runs. Limit the
- # removable XML to tags inside one paragraph so unrelated text is untouched.
- split_run_gap = (
- r"(?:(?:(?!?w:p(?:\s|>))<[^>]+>)*]*)?>)?"
- )
+ # Word may insert proofing marks, bookmarks or run formatting between
+ # text nodes. Paragraph boundaries, tabs and line breaks end a delimiter.
+ boundary = r"?w:(?:p|tab|br|cr)(?=[\s/>])"
+ split_run_gap = rf"(?:(?:(?!{boundary})<[^>]+>)*]*)?>)?"
for escaped_delimiter in ("{_{", "}_}", "{_%", "%_}"):
pattern = split_run_gap.join(map(re.escape, escaped_delimiter))
src_xml = re.sub(pattern, escaped_delimiter, src_xml)
diff --git a/docs/index.rst b/docs/index.rst
index a0c5763..fb5eed5 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -186,6 +186,9 @@ In order to display ``{%``, ``%}``, ``{{`` or ``}}``, one can use::
{_%, %_}, {_{ or }_}
+The characters of an escaped delimiter can have different Word formatting.
+A tab or line break between the characters interrupts the delimiter and is preserved.
+
Tables
......
diff --git a/tests/unit/test_render.py b/tests/unit/test_render.py
index 82b32d7..694a888 100644
--- a/tests/unit/test_render.py
+++ b/tests/unit/test_render.py
@@ -1,4 +1,7 @@
import pytest
+from docx import Document
+from docx.oxml import OxmlElement
+from docx.oxml.ns import qn
from jinja2 import Environment
from jinja2.exceptions import TemplateSyntaxError, UndefinedError
from jinja2 import StrictUndefined
@@ -15,6 +18,41 @@ def test_variables(make_docx):
assert body_text(saved(tpl)) == ["Hello Ada", "Total: $12"]
+@pytest.mark.parametrize(
+ ("separator", "expected"), [("", "}}"), ("\t", "}\t_}"), ("\n", "}\n_}")]
+)
+def test_escaped_delimiter_across_runs(tmp_path, separator, expected):
+ doc = Document()
+ paragraph = doc.add_paragraph()
+ paragraph.add_run("left }")
+ paragraph.add_run(separator)
+ paragraph.add_run("_").bold = True
+ paragraph.add_run("} right")
+ path = tmp_path / "delimiter.docx"
+ doc.save(path)
+
+ tpl = DocxTemplate(path)
+ tpl.render({})
+ assert body_text(saved(tpl)) == ["left " + expected + " right"]
+
+
+def test_escaped_delimiter_across_proofing_marks(tmp_path):
+ doc = Document()
+ paragraph = doc.add_paragraph()
+ paragraph.add_run("left }")
+ for mark, text in (("spellStart", "_"), ("spellEnd", "} right")):
+ marker = OxmlElement("w:proofErr")
+ marker.set(qn("w:type"), mark)
+ paragraph._p.append(marker)
+ paragraph.add_run(text)
+ path = tmp_path / "proofing.docx"
+ doc.save(path)
+
+ tpl = DocxTemplate(path)
+ tpl.render({})
+ assert body_text(saved(tpl)) == ["left }} right"]
+
+
def test_missing_variable_renders_empty(make_docx):
tpl = DocxTemplate(make_docx(["Hi {{ missing }}!"]))
tpl.render({})
@@ -142,7 +180,6 @@ def test_save_to_path(make_docx, tmp_path):
tpl.render({"a": "ok"})
out = tmp_path / "out.docx"
tpl.save(out)
- from docx import Document
assert body_text(Document(str(out))) == ["ok"]