![从左到右截断文本](https://linux22.com/image/298629/%E4%BB%8E%E5%B7%A6%E5%88%B0%E5%8F%B3%E6%88%AA%E6%96%AD%E6%96%87%E6%9C%AC.png)
我简单地需要截断一个文本字符串,它只是一串没有空格的字符,因此换行不起作用。我只需要确定字符串的长度(以厘米为单位),其余部分应该被截断。
我找到了包裹修剪夹可以做到这一点,但我需要一种不使用此包来实现它的方法(我仍然可以使用现有的、常见的包)
我希望还有其他可能性。我找到了一个非常好的方法如果你想从右侧剪断绳子,就这样做。但我需要从另一侧剪断。
我将给出一个简短的例子,说明我的文档应如何:
\documentclass{article}
\newcommand\magicCommand[1]{
...
}
\begin{document}
\begin{table}[htb]
\begin{center}
\begin{tabular}{l}
\begin{tabular}{|ll|}
\hline
\parbox{50mm}{ \magicCommand{3cm}{veryLongStringWhichIsGoingToLookUglyInMyDocument}} & \parbox{40mm}{ abc }\\
\hline
\end{tabular}
\end{tabular}
\end{center}
\end{table}
\end{document}
上述示例的文本在文档中应如下所示:
veryLongStri
\编辑
抱歉这么长,但我真的很困惑。truncate
似乎有某种错误(取决于字符串),如您所见,我做了一些其他测试。有些解决方案似乎可以正常工作。我不知道之前遇到了什么问题,因为有些版本只在表格环境之外工作。
test.tex
\documentclass[10pt]{article}
\usepackage{a4,graphicx}
\usepackage[breakwords]{truncate}
\setlength{\oddsidemargin}{0cm}
\setlength{\evensidemargin}{0cm}
\setlength{\topmargin}{-2.0cm}
\setlength{\textwidth}{16cm}
\setlength{\textheight}{28.65cm}
\setlength{\headsep}{1.0cm}
\setlength{\parindent}{1.0cm}
\newcommand{\singlespace}{\renewcommand{\baselinestretch}{1.0}\small\normalsize}\newcommand{\doublespace}{\renewcommand{\baselinestretch}{1.5}\small\normalsize}
\renewcommand{\floatpagefraction}{0.95}
\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.95}
\renewcommand{\bottomfraction}{0.95}
%===new===
\newlength{\stringlen}
\newbox\stringbox
\newcommand{\clipstring}[2][6cm]{%
\setlength\stringlen{#1}%
\sbox\stringbox{}%
\doclipstring#2\doclipstring
}
\makeatletter
\newcommand{\doclipstring}[1]{%
\ifx#1\doclipstring
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\unhbox\stringbox}%
{\check@cliplength{#1}}%
}
\def\check@cliplength#1{%
\sbox\stringbox{\unhcopy\stringbox #1}
\ifdim\wd\stringbox>\stringlen
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\unhbox\stringbox\removetrailing}%
{\doclipstring}%
}
\def\removetrailing#1\doclipstring{}
\makeatother
%=========
\usepackage{xcolor}
\newcommand\tleft[3][white]{%
\setbox0=\hbox{\x}%
\makebox[#2][l]{%
\ooalign{\mbox{#3}\cr\kern#2\textcolor{#1}{%
\rule[-\dp0]{\wd0}{\dimexpr\dp0+\ht0}}}}%
}
%=========
\makeatletter
\newcommand\jw[2][2cm]{%
\leavevmode
\begingroup
\setbox0=\hbox{#2}%
\setbox0=\hbox{%
\pdfsave
\pdfliteral{%
0 \dim@in@bp{-\dp0 } \dim@in@bp{#1} \dim@in@bp{\ht0 + \dp0 }
re W n}%
\rlap{\box0 }%
\pdfrestore
\hskip#1 %
}%
\box0 %
\endgroup
}
\newcommand\dim@in@bp[1]{%
\strip@pt\dimexpr(#1) * 800/803\relax
}
\makeatother
%=========
\begin{document}
\pagestyle{empty}
\begin{table}[htb]
\begin{center}
\begin{tabular}{l}
\begin{tabular}{|ll|}
\hline
\parbox{60mm}{ thoseAreTheVeryLongStringsWeWantToCutOtherwiseTheTableLooksLikeCrap\_bad }& \parbox{80mm}{ 0.031 }\\
\parbox{60mm}{ \truncate{6cm}{qwfioqoiwefjiuwgwejfmqefiuhSiqwdMwqkdwqmkMqdwmDWmdwklqwdmlDMlkqwlkdMDLWQd} }& \parbox{80mm}{ 2.047 }\\
\parbox{60mm}{ \truncate{6cm}{qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\_qqqq} }& \parbox{80mm}{ 2.047 }\\
\parbox{60mm}{ \truncate{6cm}{aaaaaaaa\_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\_qqqq} }& \parbox{80mm}{ 2.047 }\\
\parbox{60mm}{ \truncate{6cm}{truncate\_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\_qqqq} }& \parbox{80mm}{ 2.047 }\\
\parbox{60mm}{ \truncate{6cm}{thoseAreTheVeryLongStringsWeWantToCutOtherwiseTheTableLooksLikeCrap\_good} }& \parbox{80mm}{ 1.023 }\\
\parbox{60mm}{ \clipstring{clipstring\_thoseAreTheVeryLongStringsWeWantToCutOtherwiseTheTableLooksLikeCrap} }& \parbox{80mm}{ 1.023 }\\
\parbox{60mm}{ \tleft{6cm}{tleft\_thoseAreTheVeryLongStringsWeWantToCutOtherwiseTheTableLooksLikeCrap} }& \parbox{80mm}{ 1.023 }\\
\parbox{60mm}{ \jw[6cm]{jw\_thoseAreTheVeryLongStringsWeWantToCutOtherwiseTheTableLooksLikeCrap} }& \parbox{80mm}{ 1.023 }\\
\parbox{60mm}{ \jw[6cm]{jw\_qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\_qqqq} }& \parbox{80mm}{ 1.023 }\\
\hline
\end{tabular}
\end{tabular}
\end{center}
\end{table}
\end{document}
testoutput
pdflatex.log
This is pdfTeXk, Version 3.141592-1.40.3 (Web2C 7.5.6)
%&-line parsing enabled.
entering extended mode
(./test.tex
LaTeX2e <2005/12/01>
Babel <v3.8h> and hyphenation patterns for english, usenglishmax, dumylang, noh
yphenation, arabic, basque, bulgarian, coptic, welsh, czech, slovak, german, ng
erman, danish, esperanto, spanish, catalan, galician, estonian, farsi, finnish,
french, greek, monogreek, ancientgreek, croatian, hungarian, interlingua, ibyc
us, indonesian, icelandic, italian, latin, mongolian, dutch, norsk, polish, por
tuguese, pinyin, romanian, russian, slovenian, uppersorbian, serbian, swedish,
turkish, ukenglish, ukrainian, loaded.
(/usr/share/texmf/tex/latex/base/article.cls
Document Class: article 2005/09/16 v1.4f Standard LaTeX document class
(/usr/share/texmf/tex/latex/base/size10.clo))
(/usr/share/texmf/tex/latex/ntgclass/a4.sty)
(/usr/share/texmf/tex/latex/graphics/graphicx.sty
(/usr/share/texmf/tex/latex/graphics/keyval.sty)
(/usr/share/texmf/tex/latex/graphics/graphics.sty
(/usr/share/texmf/tex/latex/graphics/trig.sty)
(/usr/share/texmf/tex/latex/config/graphics.cfg)
(/usr/share/texmf/tex/latex/pdftex-def/pdftex.def)))
(/usr/share/texmf/tex/latex/ltxmisc/truncate.sty)
(/usr/share/texmf/tex/latex/xcolor/xcolor.sty
(/usr/share/texmf/tex/latex/config/color.cfg)) (./test.aux)
Overfull \hbox (194.49529pt too wide) in paragraph at lines 96--96
[]\OT1/cmr/m/n/10 thoseAreTheVeryLongStringsWeWantToCutOtherwiseTheTableLooksLi
keCrap[]bad
Overfull \hbox (207.60631pt too wide) detected at line 99
\OT1/cmr/m/n/10 qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qqqq[]qqqq
Overfull \hbox (251.20644pt too wide) detected at line 100
\OT1/cmr/m/n/10 aaaaaaaa[]qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqq[]qqqq
Overfull \hbox (3.02272pt too wide) in paragraph at lines 104--104
[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][
]\OT1/cmr/m/n/10 c[]l[]i[]p[]s[]t[]r[]i[]n[]g[][][]t[]h[]o[]s[]e[]A[]r[]e[]T[]h
[]e[]V[]e[]r[]y[]L[]o[]n[]g[]S[]t[]r[]i[]n[]g[]s[]
! Undefined control sequence.
\\tleft [#1]#2#3->\setbox 0=\hbox {\x
}\makebox [#2][l]{\ooalign {\mbox {#3}...
l.106 ...antToCutOtherwiseTheTableLooksLikeCrap} }
& \parbox{80mm}{ 1.023 }\\
? [1{/usr/share/texmf/fonts/map/pdftex/updmap/pdftex.map}] (./test.aux) )</usr/sh
are/texmf/fonts/type1/bluesky/cm/cmr10.pfb>
Output written on test.pdf (1 page, 12123 bytes).
Transcript written on test.log.
答案1
使用truncate
包
\documentclass{article}
\usepackage[breakwords]{truncate}
\begin{document}
\truncate{3cm}{This text has been~truncated}
\end{document}
该包使用了一种基于连字符的巧妙策略。但需要注意的是,如果您只是插入很长的无意义字母序列,则可能会遇到找不到断点的问题。使用合理的文本效果很好。请阅读文档。texdoc truncate
从命令行使用是最快的方法。
答案2
当进行大多数盒子操作时,您可以参考\width
包含盒子自然宽度的内容。
\documentclass{article}
\usepackage{trimclip}
\newcommand\clipstring[2][3cm]{%
\clipbox{0pt 0pt {\dimexpr\width-#1\relax} 0pt}{#2}%
}
\begin{document}
\clipstring{veryLongStringWhichIsGoingToLookUglyInMyDocument} is
not too long any more
\makebox[3cm]{\hrulefill} is not too long any more
\end{document}
我为 添加了可选参数\clipstring
,以便您可以在运行时使用 调整长度\clipstring[2cm]{...}
。
您可能需要一个以打字机类型剪辑字符串的宏,并指定要保留的字符数:
\documentclass{article}
\usepackage{trimclip}
\newcommand\clipstring[2][3cm]{%
\clipbox{0pt 0pt {\dimexpr\width-#1\relax} 0pt}{#2}%
}
\newcommand\clipttstring[2][10]{%
\texttt{\clipbox{0pt 0pt {\dimexpr\width-(#1em)/2\relax} 0pt}{#2}}%
}
\begin{document}
\clipstring{veryLongStringWhichIsGoingToLookUglyInMyDocument} is
not too long any more
\makebox[3cm]{\hrulefill} is not too long any more % check
\clipttstring{veryLongStringWhichIsGoingToLookUglyInMyDocument} is
not too long any more
\texttt{0123456789} is not too long any more % check
\end{document}
默认值为 10,用于\clipttstring[6]{...}
仅保留 6 个字符。
相反,这是一个宏,当超出固定长度时,它将停止打印字符。
\documentclass{article}
\newlength{\stringlen}
\newbox\stringbox
\newcommand{\clipstring}[2][3cm]{%
\setlength\stringlen{#1}%
\sbox\stringbox{}%
\doclipstring#2\doclipstring
}
\makeatletter
\newcommand{\doclipstring}[1]{%
\ifx#1\doclipstring
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\unhbox\stringbox}%
{\check@cliplength{#1}}%
}
\def\check@cliplength#1{%
\sbox\stringbox{\unhcopy\stringbox #1}
\ifdim\wd\stringbox>\stringlen
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
{\unhbox\stringbox\removetrailing}%
{\doclipstring}%
}
\def\removetrailing#1\doclipstring{}
\makeatother
\begin{document}
\clipstring{veryLongStringWhichIsGoingToLookUglyInMyDocument} is
not too long any more
\makebox[3cm]{\hrulefill} is not too long any more % check
\clipstring{shrt} is short
\end{document}
限制:参数必须是简单 ASCII 字符的字符串;但是不能有空格,也不能有重音字母。
类似的解决方案,但计算要保留的字符(默认为十个)。
\documentclass{article}
\makeatletter
\newcommand\stringclip[2][10]{%
\@tempcnta=#1\relax
\@tempcntb=\z@
\toks@={}%
\do@stringclip#2\do@stringclip
}
\def\do@stringclip#1{%
\ifx#1\do@stringclip
\the\toks@\expandafter\@gobble
\else
\advance\@tempcntb\@ne
\expandafter\@firstofone
\fi
{\check@one@more{#1}}%
}
\def\check@one@more#1{%
\ifnum\@tempcntb=\@tempcnta
\the\toks@ #1%
\expandafter\@firstoftwo
\else
\toks@=\expandafter{\the\toks@ #1}%
\expandafter\@secondoftwo
\fi
{\gobble@toend}%
{\do@stringclip}%
}
\def\gobble@toend#1\do@stringclip{}
\makeatother
\begin{document}
\stringclip{01234567890123456789}
\stringclip[5]{01234567890123456789}
\stringclip{012345}
\end{document}
答案3
访问了一个使用驱动程序级别框裁剪的解决方案通过 expl3
(界面为egreg 的版本):
\documentclass{article}
\usepackage{expl3,xparse}
\ExplSyntaxOn
\NewDocumentCommand \clipstring { O { 2cm } m }
{
\erp_clip:nn {#2} {#1}
}
\box_new:N \l__erp_box
\cs_new_protected:Npn \erp_clip:nn #1#2
{
\leavevmode
\hbox_set:Nn \l__erp_box {#1}
\box_set_wd:Nn \l__erp_box {#2}
\box_clip:N \l__erp_box
\box_use:N \l__erp_box
}
\ExplSyntaxOff
\begin{document}
\clipstring[3cm]{veryLongStringWhichIsGoingToLookUglyInMyDocument} is
not too long any more
\makebox[3cm]{\hrulefill} is not too long any more % check
\end{document}
如果绝对不允许使用任何软件包,我们可以通过“手动”重新编码低级剪辑来做同样的事情。这与一个驱动程序有关,但这可能是可以接受的。例如,使用 pdfTeX 或 LuaTeX 直接制作 PDF,上面的“写出”是
\documentclass{article}
\makeatletter
\newcommand\clipstring[2][2cm]{%
\leavevmode
\begingroup
\setbox0=\hbox{#2}%
\setbox0=\hbox{%
\pdfsave
\pdfliteral{%
0 \dim@in@bp{-\dp0 } \dim@in@bp{#1} \dim@in@bp{\ht0 + \dp0 }
re W n}%
\rlap{\box0 }%
\pdfrestore
\hskip#1 %
}%
\box0 %
\endgroup
}
\newcommand\dim@in@bp[1]{%
\strip@pt\dimexpr(#1) * 800/803\relax
}
\makeatother
\begin{document}
\clipstring[2.95cm]{veryLongStringWhichIsGoingToLookUglyInMyDocument} is
not too long any more
\makebox[2.95cm]{\hrulefill} is not too long any more % check
\end{document}
(我做了一些小改动,因为在这种情况下我可以对一些东西进行硬编码,而在编写驱动程序代码时则无法做到expl3
!)
答案4
这使用xcolor
而不是trimclip
。正如 egreg 正确指出的那样,我的原始答案在彩色背景上不起作用。所以我编辑了它以添加一个可选参数,其中可以指定背景颜色(默认白色)。
已编辑,用 (oops!)替换定义\x
内的局部变量。\tleft
#3
\documentclass{article}
\usepackage{xcolor}
\newcommand\tleft[3][white]{%
\setbox0=\hbox{#3}%
\makebox[#2][l]{%
\ooalign{\mbox{#3}\cr\kern#2\textcolor{#1}{%
\rule[-\dp0]{\wd0}{\dimexpr\dp0+\ht0}}}}%
}
\begin{document}
\def\x{This is my very long string}
\x
\rule{1.4cm}{2pt}
\tleft{1.4cm}{\x}NEXT
\end{document}