![表格单元格中的文本换行](https://linux22.com/image/438474/%E8%A1%A8%E6%A0%BC%E5%8D%95%E5%85%83%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%E6%8D%A2%E8%A1%8C.png)
我有以下代码:
\documentclass[parskip=half-]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[]{microtype}
\usepackage{hyperref}
\usepackage{geometry}
\usepackage{ragged2e}
\usepackage{makecell,xltabular}
\justifying
\geometry{
a4paper,
% includeheadfoot,
left=15mm,
right=15mm,
top=25mm,
bottom=25mm,
}
\usepackage[headsepline,plainheadsepline,manualmark]{scrlayer-scrpage}
\usepackage[bottom]{footmisc}
\begin{document}
\ttfamily
\begin{xltabular}{\linewidth}{|p{0.2\linewidth}p{0.2\linewidth}lX|}
\hline {\bfseries Address} & {\bfseries Name} & {\bfseries Value} & {\bfseries Description}\\\hline\hline\endfirsthead
\multicolumn{4}{r}{continued from previous page} \\
\hline {\bfseries Address} & {\bfseries Name} & {\bfseries Value} & {\bfseries Description}\\\hline\hline\endhead
\multicolumn{4}{r}{continued on next page} \\\endfoot
\endlastfoot
0x0000 & \hyperref[mylabel]{AAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBB} & 0x00000000 & General purpose control register General purpose control register General purpose control register General purpose control register\\\hline
\end{xltabular}
\label{mylabel}
\end{document}
第二列有一个长单词,没有换行。
我如何让它换行并包含在单元格内?我也不想附加连字符。
答案1
我建议您加载seqsplit
包并将长字符串包含在\seqsplit
指令中,或者加载xurl
包并将长字符串包含在\path
指令中;\nolinkurl
也可以。
\documentclass[parskip=half-]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{microtype}
\usepackage{geometry}
\usepackage{ragged2e}
\usepackage{makecell,xltabular}
\justifying
\geometry{a4paper,left=15mm,right=15mm,vmargin=25mm}
\usepackage[headsepline,plainheadsepline,manualmark]{scrlayer-scrpage}
\usepackage[bottom]{footmisc}
%% new instructions
\newcolumntype{L}{>{\RaggedRight}X}
\newcolumntype{P}[1]{>{\RaggedRight}p{#1\linewidth}}
\usepackage{seqsplit} % <-- new
\usepackage{xurl} % <-- new
\usepackage{hyperref} % <-- load this package last
\begin{document}
\ttfamily
\begin{xltabular}{\linewidth}{| P{0.2} P{0.2} l L |}
%% headers and footers
\hline
\textbf{Address} & \textbf{Name} & \textbf{Value} & \textbf{Description}\\
\hline\hline
\endfirsthead
\multicolumn{4}{@{}l}{(continued from previous page)} \\
\hline
\textbf{Address} & \textbf{Name} & \textbf{Value} & \textbf{Description}\\
\hline\hline
\endhead
\hline
\multicolumn{4}{r@{}}{(continued on next page)} \\
\endfoot
\hline
\endlastfoot
%% body of table
0x0000
& \hyperref[mylabel]{\seqsplit{AAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBB}}
& 0x00000000
& General purpose control register
General purpose control register
General purpose control register
General purpose control register\\
\hline
0x0000
& \hyperref[mylabel]{\path{AAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBB}}
& 0x00000000
& General purpose control register
General purpose control register
General purpose control register
General purpose control register\\
\end{xltabular}
\label{mylabel} % <-- this instruction won't be associated with the table
% unless you also provide a \caption directive
\end{document}