Markdown 表格在渲染的 PDF 中溢出

Markdown 表格在渲染的 PDF 中溢出

我有一个包含许多 pipetable 的 markdown 文件。它链接到一个 tex 文件中。

大多数情况下,文本对于一行来说太长了。当我用创建 pdf 时,pdflatex --shell-escape ./test.tex我以为当文本对于页面来说太长时会自动换行,但事实并非如此。表格溢出了页面边框,部分内容不再可见。

这是一个说明该问题的小例子:

测试.tex

\documentclass{article}
\usepackage{markdown}
\def\markdownOptionPipeTables{true}

\begin{document}

\markdownInput{test.md}

\end{document}

测试.md

# Test table 

|||Euro|
|:---|:---|---:|
| 1. | This is a very long line that should not overflow the page borders. But it does. I didn't found a solution to get a line break done.  | 10,00 Euro |

答案1

https://github.com/Witiko/markdown/issues/113#issuecomment-1002814678,该包的作者markdown为您的问题提供了解决方案。他定义了一个使用该tabularx环境的自定义表格渲染器。

根据您的具体示例,您可以使用以下内容:

\documentclass{article}

\usepackage{tabularx}
\usepackage{ragged2e}
\usepackage{markdown}

\markdownSetup{pipeTables = true}

\makeatletter
\newcount\markdownLaTeXRowCounter
\newcount\markdownLaTeXRowTotal
\newcount\markdownLaTeXColumnCounter
\newcount\markdownLaTeXColumnTotal
\newtoks\markdownLaTeXTable
\newtoks\markdownLaTeXTableAlignment
\newtoks\markdownLaTeXTableEnd

\markdownSetup{
  renderers = {
    table = {%
      \markdownLaTeXTable={}%
      \markdownLaTeXTableAlignment={}%
      \markdownLaTeXTableEnd={%
        \end{tabularx}%
      }%
      \ifx\empty#1\empty\else
        \addto@hook\markdownLaTeXTable{%
          \begin{table}
          \caption{#1}
          \centering
        }%
        \addto@hook\markdownLaTeXTableEnd{%
          \end{table}%
        }%
      \fi
      \addto@hook\markdownLaTeXTable{
        \begin{tabularx}{\linewidth}%
      }%
      \markdownLaTeXRowCounter=0%
      \markdownLaTeXRowTotal=#2%
      \markdownLaTeXColumnTotal=#3%
      \markdownLaTeXRenderTableRow
    }
  }
}

\def\markdownLaTeXRenderTableRow#1{%
  \markdownLaTeXColumnCounter=0%
  \ifnum\markdownLaTeXRowCounter=0\relax
    \markdownLaTeXReadAlignments#1%
    \markdownLaTeXTable=\expandafter\expandafter\expandafter{%
      \expandafter\the\expandafter\markdownLaTeXTable\expandafter{%
        \the\markdownLaTeXTableAlignment
      }%
    }%
    \addto@hook\markdownLaTeXTable{\hline}%
  \else
    \markdownLaTeXRenderTableCell#1%
    \addto@hook\markdownLaTeXTable{\hline}%
  \fi
  \advance\markdownLaTeXRowCounter by 1\relax
  \ifnum\markdownLaTeXRowCounter>\markdownLaTeXRowTotal\relax
    \expandafter\the\expandafter\markdownLaTeXTable
    \the\markdownLaTeXTableEnd
    \expandafter\@gobble
  \fi\markdownLaTeXRenderTableRow
}

\def\markdownLaTeXReadAlignments#1{%
  \advance\markdownLaTeXColumnCounter by 1\relax
  \if#1d%
    \addto@hook\markdownLaTeXTableAlignment{|X<{\RaggedRight}}%
  \else
    \addto@hook\markdownLaTeXTableAlignment{|#1<{\RaggedRight}}%
  \fi
  \ifnum\markdownLaTeXColumnCounter<\markdownLaTeXColumnTotal
    \relax
  \else
    \addto@hook\markdownLaTeXTableAlignment{|}%
    \expandafter\@gobble
  \fi\markdownLaTeXReadAlignments
}

\def\markdownLaTeXRenderTableCell#1{%
  \advance\markdownLaTeXColumnCounter by 1\relax
  \ifnum\markdownLaTeXColumnCounter<\markdownLaTeXColumnTotal\relax
    \addto@hook\markdownLaTeXTable{#1&}%
  \else
    \addto@hook\markdownLaTeXTable{#1\\}%
    \expandafter\@gobble
  \fi\markdownLaTeXRenderTableCell
}
\makeatother

\begin{document}

\begin{markdown}
# Test table 

|||Euro|
|:---|---|---:|
| 1. | This is a very long line that should not overflow the page borders. But it does. I didn't found a solution to get a line break done.  | 10,00 Euro |
\end{markdown}

\end{document}

请注意,第二列不再左对齐(与原始示例---相比)。:---

在此处输入图片描述

相关内容