diff --git a/demo.md b/demo.md
--- a/demo.md
+++ b/demo.md
@@ -36,8 +36,13 @@
 
 # Chapter 2. Equations {#sec:sec2}
 
+Display equations are labelled and numbered
+
 $$ P_i(x) = \sum_i a_i x^i $$ {#eq:eqn1}
 
+Since 0.1.6.0 those can also appear in the middle of paragraph
+$$a x^2 + b x^2 + c = 0$${#eq:quadr} like this.
+
 # Chapter 3. Tables
 
 | First Header | Second Header |
@@ -97,7 +102,6 @@
 # Unnumbered chapter. {-}
 
 This chapter doesn't change chapter prefix of referenced elements, instead keeping number of previous chapter, e.g.
-
 $$ S(x) = \int_{x_1}^{x_2} a x+b \  \mathrm{d}x $$ {#eq:eqn2}
 
 # Chapter 5. Reference lists
diff --git a/pandoc-crossref.cabal b/pandoc-crossref.cabal
--- a/pandoc-crossref.cabal
+++ b/pandoc-crossref.cabal
@@ -2,7 +2,7 @@
 -- documentation, see http://haskell.org/cabal/users-guide/
 
 name:                pandoc-crossref
-version:             0.1.5.6
+version:             0.1.6.0
 synopsis:            Pandoc filter for cross-references
 description:         pandoc-crossref is a pandoc filter for numbering figures, equations, tables and cross-references to them.
 license:             GPL-2
@@ -24,7 +24,7 @@
 source-repository this
   type: git
   location: https://github.com/lierdakil/pandoc-crossref
-  tag: v0.1.5.6
+  tag: v0.1.6.0
 
 library
   exposed-modules:     Text.Pandoc.CrossRef
diff --git a/src/Text/Pandoc/CrossRef/References.hs b/src/Text/Pandoc/CrossRef/References.hs
--- a/src/Text/Pandoc/CrossRef/References.hs
+++ b/src/Text/Pandoc/CrossRef/References.hs
@@ -2,6 +2,6 @@
 
 import Text.Pandoc.CrossRef.References.Accessors as X
 import Text.Pandoc.CrossRef.References.Types as X
-import Text.Pandoc.CrossRef.References.Blocks as X
+import Text.Pandoc.CrossRef.References.Blocks as X (divBlocks, replaceBlocks)
 import Text.Pandoc.CrossRef.References.Refs as X
 import Text.Pandoc.CrossRef.References.List as X
diff --git a/src/Text/Pandoc/CrossRef/References/Blocks.hs b/src/Text/Pandoc/CrossRef/References/Blocks.hs
--- a/src/Text/Pandoc/CrossRef/References/Blocks.hs
+++ b/src/Text/Pandoc/CrossRef/References/Blocks.hs
@@ -1,6 +1,12 @@
-module Text.Pandoc.CrossRef.References.Blocks (divBlocks, replaceBlocks) where
+module Text.Pandoc.CrossRef.References.Blocks
+  ( divBlocks
+  , replaceBlocks
+  , spanInlines
+  , replaceInlines
+  ) where
 
 import Text.Pandoc.Definition
+import Text.Pandoc.Generic
 import Text.Pandoc.Builder (text, toList)
 import Text.Pandoc.Shared (stringify, normalizeSpaces)
 import Control.Monad.State
@@ -40,16 +46,6 @@
             RawInline (Format "tex") ("\\label{"++label++"}") : alt
           _  -> applyTemplate idxStr alt $ figureTemplate opts
     return $ Para [Image alt' img]
-replaceBlocks opts (Div (label,_,attrs) [Plain [Math DisplayMath eq]])
-  | "eq:" `isPrefixOf` label
-  = case outFormat opts of
-      f | isFormat "latex" f ->
-        let eqn = "\\begin{equation}"++eq++"\\label{"++label++"}\\end{equation}"
-        in return $ Para [RawInline (Format "tex") eqn]
-      _ -> do
-        idxStr <- replaceAttr opts label (lookup "label" attrs) [] eqnRefs'
-        let eq' = eq++"\\qquad("++stringify idxStr++")"
-        return $ Para [Math DisplayMath eq']
 replaceBlocks opts (Div (label,_,attrs) [Table title align widths header cells])
   | not $ null title
   , "tbl:" `isPrefixOf` label
@@ -110,20 +106,37 @@
             Para caption'
           , CodeBlock ([], classes, attrs) code
           ]
-replaceBlocks _ x = return x
+replaceBlocks opts x = bottomUpM (replaceInlines opts) x
 
+replaceInlines :: Options -> [Inline] -> WS [Inline]
+replaceInlines opts (Span (label,_,attrs) [Math DisplayMath eq]:ils')
+  | "eq:" `isPrefixOf` label
+  = case outFormat opts of
+      f | isFormat "latex" f ->
+        let eqn = "\\begin{equation}"++eq++"\\label{"++label++"}\\end{equation}"
+        in return $ RawInline (Format "tex") eqn : ils'
+      _ -> do
+        idxStr <- replaceAttr opts label (lookup "label" attrs) [] eqnRefs'
+        let eq' = eq++"\\qquad("++stringify idxStr++")"
+        return $ Math DisplayMath eq' : ils'
+replaceInlines _ x = return x
+
 divBlocks :: Block -> Block
 divBlocks (Para (Image alt (img, title):c))
   | Just label <- getRefLabel "fig" c
   = Div (label,[],[]) [Plain [Image alt (img, "fig:" ++ title)]]
-divBlocks (Para (math@(Math DisplayMath _eq):c))
-  | Just label <- getRefLabel "eq" c
-  = Div (label,[],[]) [Plain [math]]
 divBlocks (Table title align widths header cells)
   | not $ null title
   , Just label <- getRefLabel "tbl" [last title]
   = Div (label,[],[]) [Table (init title) align widths header cells]
-divBlocks x = x
+divBlocks x = bottomUp spanInlines x
+
+spanInlines :: [Inline] -> [Inline]
+spanInlines (math@(Math DisplayMath _eq):ils)
+  | c:ils' <- dropWhile (==Space) ils
+  , Just label <- getRefLabel "eq" [c]
+  = Span (label,[],[]) [math]:ils'
+spanInlines x = x
 
 getRefLabel :: String -> [Inline] -> Maybe String
 getRefLabel _ [] = Nothing
diff --git a/test/Native.hs b/test/Native.hs
--- a/test/Native.hs
+++ b/test/Native.hs
@@ -18,7 +18,9 @@
   ,Para [Image [Str "Figure",Space,Str "#",Space,Str "4:",Space,Str "Third",Space,Str "figure"] ("img3.jpg","fig:")]
   ,Para [Image [Str "Unlabelled",Space,Str "image"] ("img1.jpg","fig:")]
   ,Header 1 ("sec:sec2",[],[]) [Str "Chapter",Space,Str "2.",Space,Str "Equations"]
+  ,Para [Str "Display",Space,Str "equations",Space,Str "are",Space,Str "labelled",Space,Str "and",Space,Str "numbered"]
   ,Para [Math DisplayMath " P_i(x) = \\sum_i a_i x^i \\qquad(1)"]
+  ,Para [Str "Since",Space,Str "0.1.6.0",Space,Str "those",Space,Str "can",Space,Str "also",Space,Str "appear",Space,Str "in",Space,Str "the",Space,Str "middle",Space,Str "of",Space,Str "paragraph",Space,Math DisplayMath "a x^2 + b x^2 + c = 0\\qquad(2)",Space,Str "like",Space,Str "this."]
   ,Header 1 ("sec:chapter-3.-tables",[],[]) [Str "Chapter",Space,Str "3.",Space,Str "Tables"]
   ,Table [Emph [Str "Table",Space,Str "1"],Str ":",Space,Str "Table",Space,Str "example"] [AlignLeft,AlignLeft] [0.0,0.0]
    [[Plain [Str "First",Space,Str "Header"]]
@@ -39,17 +41,22 @@
   ,Para [Str "There",Space,Str "are",Space,Str "a",Space,Str "couple",Space,Str "options",Space,Str "for",Space,Str "code",Space,Str "block",Space,Str "labels.",Space,Str "Those",Space,Str "work",Space,Str "only",Space,Str "if",Space,Str "code",Space,Str "block",Space,Str "id",Space,Str "starts",Space,Str "with",Space,Code ("",[],[]) "lst:",Str ",",Space,Str "e.g.",Space,Code ("",[],[]) "{#lst:label}"]
   ,Header 2 ("sec:caption-attr",[],[]) [Code ("",[],[]) "caption",Space,Str "attribute"]
   ,Para [Code ("",[],[]) "caption",Space,Str "attribute",Space,Str "will",Space,Str "be",Space,Str "treated",Space,Str "as",Space,Str "code",Space,Str "block",Space,Str "caption.",Space,Str "If",Space,Str "code",Space,Str "block",Space,Str "has",Space,Str "both",Space,Str "id",Space,Str "and",Space,Code ("",[],[]) "caption",Space,Str "attributes,",Space,Str "it",Space,Str "will",Space,Str "be",Space,Str "treated",Space,Str "as",Space,Str "numbered",Space,Str "code",Space,Str "block."]
-  ,Div ("lst:captionAttr",["listing","haskell"],[]) [Para [Str "Listing",Space,Str "1:",Space,Str "Listing",Space,Str "caption"],CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
+  ,Div ("lst:captionAttr",["listing","haskell"],[])
+   [Para [Str "Listing",Space,Str "1:",Space,Str "Listing",Space,Str "caption"]
+   ,CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
   ,Para [RawInline (Format "tex") "\\pagebreak"]
   ,Header 2 ("sec:table-capts",[],[]) [Str "Table-style",Space,Str "captions"]
   ,Para [Str "Enabled",Space,Str "with",Space,Code ("",[],[]) "codeBlockCaptions",Space,Str "metadata",Space,Str "option.",Space,Str "If",Space,Str "code",Space,Str "block",Space,Str "is",Space,Str "immediately",Space,Str "adjacent",Space,Str "to",Space,Str "paragraph,",Space,Str "starting",Space,Str "with",Space,Code ("",[],[]) "Listing:",Space,Str "or",Space,Code ("",[],[]) ":",Str ",",Space,Str "said",Space,Str "paragraph",Space,Str "will",Space,Str "be",Space,Str "treated",Space,Str "as",Space,Str "code",Space,Str "block",Space,Str "caption."]
-  ,Div ("lst:tableCaption",["listing","haskell"],[]) [Para [Str "Listing",Space,Str "2:",Space,Str "Listing",Space,Str "caption"],CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
+  ,Div ("lst:tableCaption",["listing","haskell"],[])
+   [Para [Str "Listing",Space,Str "2:",Space,Str "Listing",Space,Str "caption"]
+   ,CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
   ,Header 2 ("sec:wrapping-div",[],[]) [Str "Wrapping",Space,Str "div"]
   ,Para [Str "Wrapping",Space,Str "code",Space,Str "block",Space,Str "without",Space,Str "label",Space,Str "in",Space,Str "a",Space,Str "div",Space,Str "with",Space,Str "id",Space,Code ("",[],[]) "lst:...",Space,Str "and",Space,Str "class,",Space,Str "starting",Space,Str "with",Space,Code ("",[],[]) "listing",Str ",",Space,Str "and",Space,Str "adding",Space,Str "paragraph",Space,Str "before",Space,Str "code",Space,Str "block,",Space,Str "but",Space,Str "inside",Space,Str "div,",Space,Str "will",Space,Str "treat",Space,Str "said",Space,Str "paragraph",Space,Str "as",Space,Str "code",Space,Str "block",Space,Str "caption."]
-  ,Div ("lst:wrappingDiv",["listing","haskell"],[]) [Para [Str "Listing",Space,Str "3:",Space,Str "Listing",Space,Str "caption"],CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
+  ,Div ("lst:wrappingDiv",["listing","haskell"],[])
+   [Para [Str "Listing",Space,Str "3:",Space,Str "Listing",Space,Str "caption"]
+   ,CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
   ,Header 1 ("sec:unnumbered-chapter.",["unnumbered"],[]) [Str "Unnumbered",Space,Str "chapter."]
-  ,Para [Str "This",Space,Str "chapter",Space,Str "doesn't",Space,Str "change",Space,Str "chapter",Space,Str "prefix",Space,Str "of",Space,Str "referenced",Space,Str "elements,",Space,Str "instead",Space,Str "keeping",Space,Str "number",Space,Str "of",Space,Str "previous",Space,Str "chapter,",Space,Str "e.g."]
-  ,Para [Math DisplayMath " S(x) = \\int_{x_1}^{x_2} a x+b \\  \\mathrm{d}x \\qquad(2)"]
+  ,Para [Str "This",Space,Str "chapter",Space,Str "doesn't",Space,Str "change",Space,Str "chapter",Space,Str "prefix",Space,Str "of",Space,Str "referenced",Space,Str "elements,",Space,Str "instead",Space,Str "keeping",Space,Str "number",Space,Str "of",Space,Str "previous",Space,Str "chapter,",Space,Str "e.g.",Space,Math DisplayMath " S(x) = \\int_{x_1}^{x_2} a x+b \\  \\mathrm{d}x \\qquad(3)"]
   ,Header 1 ("sec:chapter-5.-reference-lists",[],[]) [Str "Chapter",Space,Str "5.",Space,Str "Reference",Space,Str "lists"]
   ,Para [Str "It's",Space,Str "also",Space,Str "possible",Space,Str "to",Space,Str "show",Space,Str "lists",Space,Str "of",Space,Str "figures",Space,Str "and",Space,Str "tables,",Space,Str "like",Space,Str "this:"]
   ,Header 2 ("list-of-figures",[],[]) [Str "List",Space,Str "of",Space,Str "Figures"]
@@ -85,7 +92,9 @@
   ,Para [Image [Str "Figure",Space,Str "#",Space,Str "1.3:",Space,Str "Third",Space,Str "figure"] ("img3.jpg","fig:")]
   ,Para [Image [Str "Unlabelled",Space,Str "image"] ("img1.jpg","fig:")]
   ,Header 1 ("sec:sec2",[],[]) [Str "Chapter",Space,Str "2.",Space,Str "Equations"]
+  ,Para [Str "Display",Space,Str "equations",Space,Str "are",Space,Str "labelled",Space,Str "and",Space,Str "numbered"]
   ,Para [Math DisplayMath " P_i(x) = \\sum_i a_i x^i \\qquad(2.1)"]
+  ,Para [Str "Since",Space,Str "0.1.6.0",Space,Str "those",Space,Str "can",Space,Str "also",Space,Str "appear",Space,Str "in",Space,Str "the",Space,Str "middle",Space,Str "of",Space,Str "paragraph",Space,Math DisplayMath "a x^2 + b x^2 + c = 0\\qquad(2.2)",Space,Str "like",Space,Str "this."]
   ,Header 1 ("sec:chapter-3.-tables",[],[]) [Str "Chapter",Space,Str "3.",Space,Str "Tables"]
   ,Table [Emph [Str "Table",Space,Str "3.1"],Str ":",Space,Str "Table",Space,Str "example"] [AlignLeft,AlignLeft] [0.0,0.0]
    [[Plain [Str "First",Space,Str "Header"]]
@@ -106,25 +115,38 @@
   ,Para [Str "There",Space,Str "are",Space,Str "a",Space,Str "couple",Space,Str "options",Space,Str "for",Space,Str "code",Space,Str "block",Space,Str "labels.",Space,Str "Those",Space,Str "work",Space,Str "only",Space,Str "if",Space,Str "code",Space,Str "block",Space,Str "id",Space,Str "starts",Space,Str "with",Space,Code ("",[],[]) "lst:",Str ",",Space,Str "e.g.",Space,Code ("",[],[]) "{#lst:label}"]
   ,Header 2 ("sec:caption-attr",[],[]) [Code ("",[],[]) "caption",Space,Str "attribute"]
   ,Para [Code ("",[],[]) "caption",Space,Str "attribute",Space,Str "will",Space,Str "be",Space,Str "treated",Space,Str "as",Space,Str "code",Space,Str "block",Space,Str "caption.",Space,Str "If",Space,Str "code",Space,Str "block",Space,Str "has",Space,Str "both",Space,Str "id",Space,Str "and",Space,Code ("",[],[]) "caption",Space,Str "attributes,",Space,Str "it",Space,Str "will",Space,Str "be",Space,Str "treated",Space,Str "as",Space,Str "numbered",Space,Str "code",Space,Str "block."]
-  ,Div ("lst:captionAttr",["listing","haskell"],[]) [Para [Str "Listing",Space,Str "4.1:",Space,Str "Listing",Space,Str "caption"],CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
+  ,Div ("lst:captionAttr",["listing","haskell"],[])
+   [Para [Str "Listing",Space,Str "4.1:",Space,Str "Listing",Space,Str "caption"]
+   ,CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
   ,Para [RawInline (Format "tex") "\\pagebreak"]
   ,Header 2 ("sec:table-capts",[],[]) [Str "Table-style",Space,Str "captions"]
   ,Para [Str "Enabled",Space,Str "with",Space,Code ("",[],[]) "codeBlockCaptions",Space,Str "metadata",Space,Str "option.",Space,Str "If",Space,Str "code",Space,Str "block",Space,Str "is",Space,Str "immediately",Space,Str "adjacent",Space,Str "to",Space,Str "paragraph,",Space,Str "starting",Space,Str "with",Space,Code ("",[],[]) "Listing:",Space,Str "or",Space,Code ("",[],[]) ":",Str ",",Space,Str "said",Space,Str "paragraph",Space,Str "will",Space,Str "be",Space,Str "treated",Space,Str "as",Space,Str "code",Space,Str "block",Space,Str "caption."]
-  ,Div ("lst:tableCaption",["listing","haskell"],[]) [Para [Str "Listing",Space,Str "4.2:",Space,Str "Listing",Space,Str "caption"],CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
+  ,Div ("lst:tableCaption",["listing","haskell"],[])
+   [Para [Str "Listing",Space,Str "4.2:",Space,Str "Listing",Space,Str "caption"]
+   ,CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
   ,Header 2 ("sec:wrapping-div",[],[]) [Str "Wrapping",Space,Str "div"]
   ,Para [Str "Wrapping",Space,Str "code",Space,Str "block",Space,Str "without",Space,Str "label",Space,Str "in",Space,Str "a",Space,Str "div",Space,Str "with",Space,Str "id",Space,Code ("",[],[]) "lst:...",Space,Str "and",Space,Str "class,",Space,Str "starting",Space,Str "with",Space,Code ("",[],[]) "listing",Str ",",Space,Str "and",Space,Str "adding",Space,Str "paragraph",Space,Str "before",Space,Str "code",Space,Str "block,",Space,Str "but",Space,Str "inside",Space,Str "div,",Space,Str "will",Space,Str "treat",Space,Str "said",Space,Str "paragraph",Space,Str "as",Space,Str "code",Space,Str "block",Space,Str "caption."]
-  ,Div ("lst:wrappingDiv",["listing","haskell"],[]) [Para [Str "Listing",Space,Str "4.3:",Space,Str "Listing",Space,Str "caption"],CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
+  ,Div ("lst:wrappingDiv",["listing","haskell"],[])
+   [Para [Str "Listing",Space,Str "4.3:",Space,Str "Listing",Space,Str "caption"]
+   ,CodeBlock ("",["haskell"],[]) "main :: IO ()\nmain = putStrLn \"Hello World!\""]
   ,Header 1 ("sec:unnumbered-chapter.",["unnumbered"],[]) [Str "Unnumbered",Space,Str "chapter."]
-  ,Para [Str "This",Space,Str "chapter",Space,Str "doesn't",Space,Str "change",Space,Str "chapter",Space,Str "prefix",Space,Str "of",Space,Str "referenced",Space,Str "elements,",Space,Str "instead",Space,Str "keeping",Space,Str "number",Space,Str "of",Space,Str "previous",Space,Str "chapter,",Space,Str "e.g."]
-  ,Para [Math DisplayMath " S(x) = \\int_{x_1}^{x_2} a x+b \\  \\mathrm{d}x \\qquad(4.1)"]
+  ,Para [Str "This",Space,Str "chapter",Space,Str "doesn't",Space,Str "change",Space,Str "chapter",Space,Str "prefix",Space,Str "of",Space,Str "referenced",Space,Str "elements,",Space,Str "instead",Space,Str "keeping",Space,Str "number",Space,Str "of",Space,Str "previous",Space,Str "chapter,",Space,Str "e.g.",Space,Math DisplayMath " S(x) = \\int_{x_1}^{x_2} a x+b \\  \\mathrm{d}x \\qquad(4.1)"]
   ,Header 1 ("sec:chapter-5.-reference-lists",[],[]) [Str "Chapter",Space,Str "5.",Space,Str "Reference",Space,Str "lists"]
   ,Para [Str "It's",Space,Str "also",Space,Str "possible",Space,Str "to",Space,Str "show",Space,Str "lists",Space,Str "of",Space,Str "figures",Space,Str "and",Space,Str "tables,",Space,Str "like",Space,Str "this:"]
   ,Header 2 ("list-of-figures",[],[]) [Str "List",Space,Str "of",Space,Str "Figures"]
-  ,Div ("",["list"],[]) [Para [Str "1",Space,Str "A",Space,Str "figure"],Para [Str "1",Str ".",Str "1",Space,Str "First",Space,Str "figure"],Para [Str "1",Str ".",Str "2",Space,Str "Second",Space,Str "figure"],Para [Str "1",Str ".",Str "3",Space,Str "Third",Space,Str "figure"]]
+  ,Div ("",["list"],[])
+   [Para [Str "1",Space,Str "A",Space,Str "figure"]
+   ,Para [Str "1",Str ".",Str "1",Space,Str "First",Space,Str "figure"]
+   ,Para [Str "1",Str ".",Str "2",Space,Str "Second",Space,Str "figure"]
+   ,Para [Str "1",Str ".",Str "3",Space,Str "Third",Space,Str "figure"]]
   ,Header 2 ("list-of-tables",[],[]) [Str "List",Space,Str "of",Space,Str "Tables"]
-  ,Div ("",["list"],[]) [Para [Str "3",Str ".",Str "1",Space,Str "Table",Space,Str "example"]]
+  ,Div ("",["list"],[])
+   [Para [Str "3",Str ".",Str "1",Space,Str "Table",Space,Str "example"]]
   ,Header 1 ("",[],[]) [Str "List",Space,Str "of",Space,Str "Listings"]
-  ,Div ("",["list"],[]) [Para [Str "4",Str ".",Str "1",Space,Str "Listing",Space,Str "caption"],Para [Str "4",Str ".",Str "2",Space,Str "Listing",Space,Str "caption"],Para [Str "4",Str ".",Str "3",Space,Str "Listing",Space,Str "caption"]]
+  ,Div ("",["list"],[])
+   [Para [Str "4",Str ".",Str "1",Space,Str "Listing",Space,Str "caption"]
+   ,Para [Str "4",Str ".",Str "2",Space,Str "Listing",Space,Str "caption"]
+   ,Para [Str "4",Str ".",Str "3",Space,Str "Listing",Space,Str "caption"]]
   ,Header 1 ("sec:appendix-a.-custom-labels",[],[("label","AppA")]) [Str "Appendix",Space,Str "A.",Space,Str "Custom",Space,Str "labels"]
   ,Header 2 ("sec:custlabs",[],[("label","CustLab")]) [Str "This",Space,Str "section",Space,Str "will",Space,Str "have",Space,Str "custom",Space,Str "label"]]
 
diff --git a/test/test-pandoc-crossref.hs b/test/test-pandoc-crossref.hs
--- a/test/test-pandoc-crossref.hs
+++ b/test/test-pandoc-crossref.hs
@@ -7,6 +7,7 @@
 import Control.Arrow
 import Data.Monoid -- needed for ghc<7.10
 import qualified Data.Map as M
+import qualified Data.Default as Df
 
 import Text.Pandoc.CrossRef
 import Text.Pandoc.CrossRef.Util.Options
@@ -27,6 +28,42 @@
 
 main :: IO ()
 main = hspec $ do
+    describe "References.Blocks.replaceInlines" $ do
+      it "Labels equations" $
+        testInlines (equation' "a^2+b^2=c^2" "equation")
+        (equation' "a^2+b^2=c^2\\qquad(1)" [],
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+      it "Labels equations in the middle of text" $
+        testInlines (
+                text "This is an equation: "
+             <> equation' "a^2+b^2=c^2" "equation"
+             <> text " it should be labeled")
+        (
+           text "This is an equation: "
+        <> equation' "a^2+b^2=c^2\\qquad(1)" []
+        <> text " it should be labeled",
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+      it "Labels equations in the beginning of text" $
+        testInlines (
+                equation' "a^2+b^2=c^2" "equation"
+             <> text " it should be labeled")
+        (
+           equation' "a^2+b^2=c^2\\qquad(1)" []
+        <> text " it should be labeled",
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+      it "Labels equations in the end of text" $
+        testInlines (
+                text "This is an equation: "
+             <> equation' "a^2+b^2=c^2" "equation")
+        (
+           text "This is an equation: "
+        <> equation' "a^2+b^2=c^2\\qquad(1)" [],
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+
+    -- TODO:
+    -- describe "References.Blocks.spanInlines"
+    -- describe "References.Blocks.divBlocks"
+
     describe "References.Blocks.replaceBlocks" $ do
       it "Labels images" $
         testBlocks (figure "test.jpg" [] "Test figure" "figure")
@@ -36,6 +73,32 @@
         testBlocks (equation "a^2+b^2=c^2" "equation")
         (equation "a^2+b^2=c^2\\qquad(1)" [],
           def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+      it "Labels equations in the middle of text" $
+        testBlocks (para $
+                text "This is an equation: "
+             <> equation' "a^2+b^2=c^2" "equation"
+             <> text " it should be labeled")
+        (para $
+           text "This is an equation: "
+        <> equation' "a^2+b^2=c^2\\qquad(1)" []
+        <> text " it should be labeled",
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+      it "Labels equations in the beginning of text" $
+        testBlocks (para $
+                equation' "a^2+b^2=c^2" "equation"
+             <> text " it should be labeled")
+        (para $
+           equation' "a^2+b^2=c^2\\qquad(1)" []
+        <> text " it should be labeled",
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
+      it "Labels equations in the end of text" $
+        testBlocks (para $
+                text "This is an equation: "
+             <> equation' "a^2+b^2=c^2" "equation")
+        (para $
+           text "This is an equation: "
+        <> equation' "a^2+b^2=c^2\\qquad(1)" [],
+          def{eqnRefs=M.fromList $ refRec'' "eq:equation" 1})
       it "Labels tables" $
         testBlocks (table' "Test table" "table")
         (table' "Table 1: Test table" [],
@@ -180,11 +243,18 @@
 testRefs'' p l1 l2 prop res = testRefs (para $ citeGen p l1) (setProp prop (refGen' p l1 l2) def) (para $ text res)
 
 testBlocks :: Blocks -> (Blocks, References) -> Expectation
-testBlocks arg res = runState (walkM (f defaultOptions) arg) def `shouldBe` res
-  where f = (. References.Blocks.divBlocks) . References.Blocks.replaceBlocks
+testBlocks = testState f def
+  where f = walkM $ References.Blocks.replaceBlocks defaultOptions . References.Blocks.divBlocks
 
+testInlines :: Inlines -> (Inlines, References) -> Expectation
+testInlines = testState (bottomUpM (References.Blocks.replaceInlines defaultOptions) . bottomUp References.Blocks.spanInlines) def
+
+testState :: (Eq s, Eq a1, Show s, Show a1, Df.Default s) =>
+               ([a] -> State s [a1]) -> s -> Many a -> (Many a1, s) -> Expectation
+testState f init' arg res = runState (f $ toList arg) init' `shouldBe` first toList res
+
 testRefs :: Blocks -> References -> Blocks -> Expectation
-testRefs bs st res = runState (bottomUpM (References.Refs.replaceRefs defaultOptions) (toList bs)) st `shouldBe` (toList res,st)
+testRefs bs st rbs = testState (bottomUpM (References.Refs.replaceRefs defaultOptions)) st bs (rbs, st)
 
 testCBCaptions :: Blocks -> Blocks -> Expectation
 testCBCaptions bs res = runState (bottomUpM (Util.CodeBlockCaptions.codeBlockCaptions defaultOptions{cbCaptions=True}) (toList bs)) def `shouldBe` (toList res,def)
@@ -199,7 +269,10 @@
 section text' level label = headerWith ("sec:" ++ label,[],[]) level (text text')
 
 equation :: String -> String -> Blocks
-equation eq ref = para (displayMath eq <> ref' "eq" ref)
+equation = (para .) . equation'
+
+equation' :: String -> String -> Inlines
+equation' eq ref = displayMath eq <> ref' "eq" ref
 
 table' :: String -> String -> Blocks
 table' title ref = table (text title <> ref' "tbl" ref) []
