如何通过添加空格来换行

如何通过添加空格来换行

我正在写一篇包含一些非常长单词的文本,但我不需要使用连字符在多行中拆分单词。我只想添加空格以适应行,如下所示。

       This is my first text for wrapping word
       in a file. This text need   to       be
       adjusted and acted based   on the table 
       which need to be  modified   and tested 
       basedonthethedata  which is given here.

我如何实现它?

答案1

正常长度的单词:

一种方法是\nohypens使用包裹hyphenat。以下是没有(左侧)和有 \nohypens(右侧)的比较:

在此处输入图片描述

笔记:

  • 如果你希望禁用整个文档的连字功能(我不建议这样做),你可以使用

    \usepackage[none]{hyphenat}
    

代码:

\documentclass{article}

\usepackage{hyphenat}

\newcommand{\Text}{%
       This is my first text for wrapping word
       in a file. This text need   to       be
       adjusted and acted based   on the table 
       which need to be  modified   and tested 
       basedonthethedata  which is given here.
}%

\begin{document}
\begin{minipage}{0.4\linewidth}
\Text
\end{minipage}%
\hspace*{2ex}
\begin{minipage}{0.4\linewidth}
\nohyphens{\Text}
\end{minipage}%
\end{document}

很长的单词:

如果单词很长,则需要指定单词中可以中断的位置。下面,我使用了如何拆分包含符号但没有连字符的长单词?在每个字母后自动添加可能的中断(该宏并不完整,但只需为每个想要允许中断的字母/字符复制代码即可)。

在此处输入图片描述

代码:

\documentclass{article}

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

% https://tex.stackexchange.com/questions/53965/how-to-break-long-word-containing-symbols-but-with-no-hyphen
\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{\hyp}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
  \leavevmode%
  \prw@zbreak%
  \discretionary{\usebox#1}{}{}%
  \prw@zbreak%
}%
\makeatother

\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
  \StrLen{#1 }[\stringLength]%
  \forloop[1]{index}{1}{\value{index}<\stringLength}{%
    \StrChar{#1}{\value{index}}[\currentLetter]%
    \IfStrEqCase{\currentLetter}{%
        % All the characters where you don't want hypen
        {a}{\currentLetter\BreakableChar[\MyBreakChar]}%
        {b}{\currentLetter\BreakableChar[\MyBreakChar]}%
        {c}{\currentLetter\BreakableChar[\MyBreakChar]}%
        {d}{\currentLetter\BreakableChar[\MyBreakChar]}%
        {e}{\currentLetter\BreakableChar[\MyBreakChar]}%
        {f}{\currentLetter\BreakableChar[\MyBreakChar]}%
        %  list other letters here.
        {z}{\currentLetter\BreakableChar[\MyBreakChar]}%
        % All the charactes where a break should have a hypen
        %{ }{\currentLetter\BreakableChar[\MySpaceBreakChar]}%
    }[\currentLetter]%
  }%
}%



\newcommand{\Text}{\setlength\emergencystretch{.5\textwidth}%
    This is my first text for wrapping word
    in a file. This text need   to       be
    adjusted and acted based   on the table 
    \AddBreakableChars{Ultramicroscopicvolcanoconiosis},
    \AddBreakableChars{Pneumonoultramicroscopicsilicovolcanoconiosis}
    which need to be  modified   and tested 
    basedonthethedata  which is given here.\par
}%

\begin{document}
\begin{minipage}{0.4\linewidth}
\Text
\end{minipage}%
\hspace*{2ex}
\begin{minipage}{0.4\linewidth}
\nohyphens{\Text}
\end{minipage}%
\end{document}

相关内容