Skip to content

Commit

Permalink
LaTeX reader: better handling of colwidths.
Browse files Browse the repository at this point in the history
Previously the parser just failed if the column width
specified in `p{}` wasn't a multiple of `\linewidth`.
This led to cases where content was skipped.
Now we treat these as ColWidthDefault and silently
parse the table.

A future improvement could be to guess relative column
widths from the dimensions specified, based on a default
line width.

Closes #9579.
  • Loading branch information
jgm committed Mar 18, 2024
1 parent a22bac2 commit a1aca22
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Text/Pandoc/Readers/LaTeX/Table.hs
Expand Up @@ -87,11 +87,12 @@ parseAligns = try $ do
let alignPrefix = symbol '>' >> braced
let alignSuffix = symbol '<' >> braced
let colWidth = try $ do
symbol '{'
ds <- trim . untokenize <$> manyTill anyTok (controlSeq "linewidth")
spaces
symbol '}'
return $ safeRead ds
ts <- braced
let isLinewidth (Tok _ (CtrlSeq "linewidth") _) = True
isLinewidth _ = False
case break isLinewidth ts of
(ds, _:_) -> return $ safeRead $ trim $ untokenize ds
_ -> return Nothing
let alignSpec = do
pref <- option [] alignPrefix
spaces
Expand Down
46 changes: 46 additions & 0 deletions test/command/9579.md
@@ -0,0 +1,46 @@
```
% pandoc -t native
\begin{tabular}{p{2in}}
\end{tabular}
# Test
\begin{tabular}{p{2\linewidth}}
\end{tabular}
^D
[ RawBlock
(Format "tex") "\\begin{tabular}{p{2in}}\n\\end{tabular}"
, Header 1 ( "test" , [] , [] ) [ Str "Test" ]
, RawBlock
(Format "tex")
"\\begin{tabular}{p{2\\linewidth}}\n\\end{tabular}"
]
```

```
% pandoc -f latex -t native
\begin{tabular}{p{2in}}
\end{tabular}
Test
\begin{tabular}{p{2\linewidth}}
\end{tabular}
^D
[ Table
( "" , [] , [] )
(Caption Nothing [])
[ ( AlignLeft , ColWidthDefault ) ]
(TableHead ( "" , [] , [] ) [])
[ TableBody ( "" , [] , [] ) (RowHeadColumns 0) [] [] ]
(TableFoot ( "" , [] , [] ) [])
, Para [ Str "Test" ]
, Table
( "" , [] , [] )
(Caption Nothing [])
[ ( AlignLeft , ColWidth 2.0 ) ]
(TableHead ( "" , [] , [] ) [])
[ TableBody ( "" , [] , [] ) (RowHeadColumns 0) [] [] ]
(TableFoot ( "" , [] , [] ) [])
]
```

0 comments on commit a1aca22

Please sign in to comment.