Latex longtable 列宽度

Latex longtable 列宽度

我刚开始学Latex,写论文,想排版一个长表格。

\begin{centering}
\begin{longtable}{p{3.5cm}p{10cm}}
AdsBot-Google&209.85.238.1,209.85.238.107,209.85.238.116,209.85.238.119,209.85.238.16,209.85.238.163,209.85.238.185,209.85.238.200,209.85.238.232,209.85.238.241,209.85.238.246,209.85.238.249,209.85.238.25,209.85.238.33,209.85.238.39,209.85.238.45,209.85.238.68,209.85.238.7,209.85.238.71,209.85.238.74,209.85.238.81,209.85.238.86,209.85.238.89,72.14.199.10,72.14.199.107,72.14.199.113,72.14.199.116,72.14.199.122,72.14.199.2,72.14.199.23,72.14.199.26,72.14.199.37,72.14.199.46,72.14.199.5,72.14.199.55,72.14.199.58,72.14.199.66,72.14.199.75,72.14.199.81,72.14.199.84,72.14.199.87,72.14.199.98\\
        AdsBot-Google-Mobile&209.85.238.206,209.85.238.232,209.85.238.241,209.85.238.68,209.85.238.86,72.14.199.100,72.14.199.115,72.14.199.122,72.14.199.23,72.14.199.26,72.14.199.46,72.14.199.5,72.14.199.81,72.14.199.84\\
        AhrefsBot/1.0&212.113.37.105,212.113.37.106,213.186.122.2\\
\end{longtable}
\end{centering}

但最后一列的宽度似乎不起作用。我用较小的长度进行了测试,它甚至显示如图所示的表格。这里出了什么问题?

在此处输入图片描述

答案1

如果你不想在文本中手动添加空格来定义可以换行的点,你可以调整解决方案当没有空格可以换行时,如何在表格中换行?将逗号定义为可断开字符:

在此处输入图片描述

笔记:

代码:

\documentclass{article}

\usepackage{longtable}
\usepackage{array}
\usepackage{showframe}

\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}
\usepackage{collcell}

\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{-}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
  \leavevmode%
  \prw@zbreak%
  \discretionary{\usebox#1}{}{}%
  \prw@zbreak%
}%

\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
  \StrLen{#1 }[\stringLength]%
  \forloop[1]{index}{1}{\value{index}<\stringLength}{%
    \StrChar{#1}{\value{index}}[\currentLetter]%
    \IfStrEq{\currentLetter}{,}
        {\currentLetter\BreakableChar[\MyBreakChar]}%
        {\currentLetter}%
  }%
}%

\newcolumntype{P}[1]{>{\collectcell\AddBreakableChars}p{#1}<{\endcollectcell}}

\begin{document}
\noindent
\begin{longtable}{@{}p{3.5cm}P{8cm}@{}}
AdsBot-Google&209.85.238.1,209.85.238.107,209.85.238.116,209.85.238.119,209.85.238.16,209.85.238.163,209.85.238.185,209.85.238.200,209.85.238.232,209.85.238.241,209.85.238.246,209.85.238.249,209.85.238.25,209.85.238.33,209.85.238.39,209.85.238.45,209.85.238.68,209.85.238.7,209.85.238.71,209.85.238.74,209.85.238.81,209.85.238.86,209.85.238.89,72.14.199.10,72.14.199.107,72.14.199.113,72.14.199.116,72.14.199.122,72.14.199.2,72.14.199.23,72.14.199.26,72.14.199.37,72.14.199.46,72.14.199.5,72.14.199.55,72.14.199.58,72.14.199.66,72.14.199.75,72.14.199.81,72.14.199.84,72.14.199.87,72.14.199.98\\
        AdsBot-Google-Mobile&209.85.238.206,209.85.238.232,209.85.238.241,209.85.238.68,209.85.238.86,72.14.199.100,72.14.199.115,72.14.199.122,72.14.199.23,72.14.199.26,72.14.199.46,72.14.199.5,72.14.199.81,72.14.199.84\\
        AhrefsBot/1.0&212.113.37.105,212.113.37.106,213.186.122.2\\
\end{longtable}%
\end{document}

相关内容