pdflatexmarkdownpandoc

Pandoc - Markdown to PDF - Table columns overlap


I have a Markdown file with text, and tables that I would like to convert to PDF. But in one of the tables the columns overlap. How can I fix this issue?

This is a MWE with the overlapping table:

Markdown file:

|    | Vulnerability   | Level   | Metric            | Deviation                                                                                   | Description                                                                                                                                                             |
|---:|:----------------|:--------|:------------------|:--------------------------------------------------------------------------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|  1 | Robustness      | major   | Fail rate = 1.000 | Adding special chars `\r` in `question` can make the model to produce unexpected outputs.   | Injecting long sequences of control characters `\r` in the value of `question` can alter the model's output significantly, producing unexpected or off-topic outputs.   |
|  2 | Robustness      | major   | Fail rate = 1.000 | Adding special chars `\x08` in `question` can make the model to produce unexpected outputs. | Injecting long sequences of control characters `\x08` in the value of `question` can alter the model's output significantly, producing unexpected or off-topic outputs. |

Command used for convertion: pandoc report_giskard.md -o demo.pdf

PDF output: PDF output from pandoc

OS: Windows 10

pdflatex --version outputs:

pdfTeX 3.141592653-2.6-1.40.26 (TeX Live 2024)
kpathsea version 6.4.0
Copyright 2024 Han The Thanh (pdfTeX) et al.
There is NO warranty.  Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.43; using libpng 1.6.43
Compiled with zlib 1.3.1; using zlib 1.3.1
Compiled with xpdf version 4.04

I want the columns to not overlap.


Solution

  • As @mb21 said, this is due to the relative number of hyphens - in the separator line. Here are a few ideas how to deals with this.

    First, we can put all tables on separate pages in landscape mode, which allows to make the tables wider. We do that with a Lua filter:

    local function latex(ltx)
      return pandoc.Blocks(pandoc.RawBlock('latex', ltx))
    end
    
    -- Make sure that the `pdflscape` package is loaded. We need it for tables.
    function Meta (meta)
      local hinc = meta['header-includes'] or List{}
      hinc:insert(latex'\\usepackage{pdflscape}')
      meta['header-includes'] = hinc
      return meta
    end
    
    -- Put tables in landscape mode.
    function Table(tbl)
      return latex'\\begin{landscape}' .. {tbl} .. latex'\\end{landscape}'
    end
    

    Unfortunately, this is not enough to fix this specific table, and a general fix is actually quite difficult.

    For a table-specific fix, we could simply hard-code the desired column widths in the filter. Here we apply those width if the table has exactly six columns, but one could create finer-grained conditions.

    function Table(tbl)
      if #tbl.colspecs == 6 then
        -- The sum of these should be 1
        local widths = {0.01, 0.11, 0.08, 0.10, 0.35, 0.35}
        tbl.colspecs = tbl.colspecs:map(function (colspec, i)
            return {colspec[1], widths[i]}
        end)
      end
      return latex'\\begin{landscape}' .. {tbl} .. latex'\\end{landscape}'
    end
    

    The output is now (somewhat) sensible looking:

    screenshot of the resulting page