From f53977e295a4c301759d54787a39d0737c155a16 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Wed, 1 Jun 2022 23:24:49 -0700
Subject: [PATCH] LaTeX writer: Improve grouping with autocites.

Closes #8088.
---
 src/Text/Pandoc/Writers/LaTeX/Citation.hs | 5 +++--
 test/command/5849-prefix.md               | 2 +-
 test/command/8088.md                      | 6 ++++++
 3 files changed, 10 insertions(+), 3 deletions(-)
 create mode 100644 test/command/8088.md

diff --git a/src/Text/Pandoc/Writers/LaTeX/Citation.hs b/src/Text/Pandoc/Writers/LaTeX/Citation.hs
index bed258614..e3c56f34c 100644
--- a/src/Text/Pandoc/Writers/LaTeX/Citation.hs
+++ b/src/Text/Pandoc/Writers/LaTeX/Citation.hs
@@ -171,8 +171,9 @@ citationsToBiblatex inlineListToLaTeX (c:cs)
       return $ text cmd <> mconcat groups
 
   where grouper prev cit = case prev of
-         ((CiteGroup oPfx oSfx ids):rest)
-             | null oSfx && null pfx -> CiteGroup oPfx sfx (cid:ids) : rest
+         ((CiteGroup oPfx [] ids):rest)
+             | null pfx && null sfx
+           -> CiteGroup oPfx sfx (cid:ids) : rest
          _ -> CiteGroup pfx sfx [cid] : prev
          where pfx = citationPrefix cit
                sfx = citationSuffix cit
diff --git a/test/command/5849-prefix.md b/test/command/5849-prefix.md
index f3217a187..fe348205e 100644
--- a/test/command/5849-prefix.md
+++ b/test/command/5849-prefix.md
@@ -20,7 +20,7 @@
 % pandoc -t latex --biblatex
 [e.g. @a1;@a2, ch.3 and elsewhere;@a3; but also @a4;@a5]
 ^D
-\autocites[e.g.][ch.3 and elsewhere]{a1,a2}{a3}[but also][]{a4,a5}
+\autocites[e.g.][]{a1}[ch.3 and elsewhere]{a2}{a3}[but also][]{a4,a5}
 ```
 ```
 % pandoc -t latex --biblatex
diff --git a/test/command/8088.md b/test/command/8088.md
new file mode 100644
index 000000000..34798ee8d
--- /dev/null
+++ b/test/command/8088.md
@@ -0,0 +1,6 @@
+```
+% pandoc -t latex --biblatex
+[@first, 1; @second; @third, 3]
+^D
+\autocites[1]{first}{second}[3]{third}
+```