Skip to content

Commit a26ec96

Browse files
committed
LaTeX writer: fix spacing issue with list in definition list.
When a list occurs at the beginning of a definition list definition, it can start on the same line as the label, which looks bad. Fix that by starting such lists with an `\item[]`.
1 parent a2d3434 commit a26ec96

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

src/Text/Pandoc/Writers/LaTeX.hs

+20-3
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ data WriterState =
7272
, stEmptyLine :: Bool -- true if no content on line
7373
, stHasCslRefs :: Bool -- has a Div with class refs
7474
, stCslHangingIndent :: Bool -- use hanging indent for bib
75+
, stIsFirstInDefinition :: Bool -- first block in a defn list
7576
}
7677

7778
startingState :: WriterOptions -> WriterState
@@ -102,7 +103,8 @@ startingState options = WriterState {
102103
, stBeamer = False
103104
, stEmptyLine = True
104105
, stHasCslRefs = False
105-
, stCslHangingIndent = False }
106+
, stCslHangingIndent = False
107+
, stIsFirstInDefinition = False }
106108

107109
-- | Convert Pandoc to LaTeX.
108110
writeLaTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
@@ -682,19 +684,25 @@ blockToLaTeX b@(RawBlock f x) = do
682684
blockToLaTeX (BulletList []) = return empty -- otherwise latex error
683685
blockToLaTeX (BulletList lst) = do
684686
incremental <- gets stIncremental
687+
isFirstInDefinition <- gets stIsFirstInDefinition
685688
beamer <- gets stBeamer
686689
let inc = if beamer && incremental then "[<+->]" else ""
687690
items <- mapM listItemToLaTeX lst
688691
let spacing = if isTightList lst
689692
then text "\\tightlist"
690693
else empty
691-
return $ text ("\\begin{itemize}" <> inc) $$ spacing $$ vcat items $$
694+
return $ text ("\\begin{itemize}" <> inc) $$
695+
spacing $$
696+
-- force list at beginning of definition to start on new line
697+
(if isFirstInDefinition then "\\item[]" else mempty) $$
698+
vcat items $$
692699
"\\end{itemize}"
693700
blockToLaTeX (OrderedList _ []) = return empty -- otherwise latex error
694701
blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do
695702
st <- get
696703
let inc = if stBeamer st && stIncremental st then "[<+->]" else ""
697704
let oldlevel = stOLLevel st
705+
isFirstInDefinition <- gets stIsFirstInDefinition
698706
put $ st {stOLLevel = oldlevel + 1}
699707
items <- mapM listItemToLaTeX lst
700708
modify (\s -> s {stOLLevel = oldlevel})
@@ -738,6 +746,8 @@ blockToLaTeX (OrderedList (start, numstyle, numdelim) lst) = do
738746
$$ stylecommand
739747
$$ resetcounter
740748
$$ spacing
749+
-- force list at beginning of definition to start on new line
750+
$$ (if isFirstInDefinition then "\\item[]" else mempty)
741751
$$ vcat items
742752
$$ "\\end{enumerate}"
743753
blockToLaTeX (DefinitionList []) = return empty
@@ -948,7 +958,14 @@ defListItemToLaTeX (term, defs) = do
948958
let term'' = if any isInternalLink term
949959
then braces term'
950960
else term'
951-
def' <- liftM vsep $ mapM blockListToLaTeX defs
961+
def' <- case concat defs of
962+
[] -> return mempty
963+
(x:xs) -> do
964+
modify $ \s -> s{stIsFirstInDefinition = True }
965+
firstitem <- blockToLaTeX x
966+
modify $ \s -> s{stIsFirstInDefinition = False }
967+
rest <- blockListToLaTeX xs
968+
return $ firstitem $+$ rest
952969
return $ case defs of
953970
((Header{} : _) : _) ->
954971
"\\item" <> brackets term'' <> " ~ " $$ def'
+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
This inserts an empty `\item[]` when a list occurs at the
2+
beginning of a definition list definition; otherwise the list
3+
may start on the line with the label, which looks terrible.
4+
See https://tex.stackexchange.com/questions/192480/force-itemize-inside-description-onto-a-new-line
5+
6+
```
7+
% pandoc -t latex
8+
Definition
9+
: 1. list
10+
2. list
11+
^D
12+
\begin{description}
13+
\item[Definition]
14+
\begin{enumerate}
15+
\def\labelenumi{\arabic{enumi}.}
16+
\tightlist
17+
\item[]
18+
\item
19+
list
20+
\item
21+
list
22+
\end{enumerate}
23+
\end{description}
24+
```
25+
26+
```
27+
% pandoc -t latex
28+
Definition
29+
: Foo
30+
31+
1. list
32+
2. list
33+
^D
34+
\begin{description}
35+
\item[Definition]
36+
Foo
37+
38+
\begin{enumerate}
39+
\def\labelenumi{\arabic{enumi}.}
40+
\tightlist
41+
\item
42+
list
43+
\item
44+
list
45+
\end{enumerate}
46+
\end{description}
47+
```
48+
49+
```
50+
% pandoc -t latex
51+
Definition
52+
: - list
53+
- list
54+
^D
55+
\begin{description}
56+
\item[Definition]
57+
\begin{itemize}
58+
\tightlist
59+
\item[]
60+
\item
61+
list
62+
\item
63+
list
64+
\end{itemize}
65+
\end{description}
66+
```
67+

0 commit comments

Comments
 (0)