表格单元格中的文本换行

表格单元格中的文本换行

我有以下代码:

\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}

相关内容