從左到右截斷文本

從左到右截斷文本

簡單地需要截斷一個文字字串,它只是一個沒有空格的字元序列,因此換行符不起作用。我只需要確定繩子的長度(以公分為單位),其餘的應該被剪掉。

我找到了包裹修剪夾可以做到這一點,但我需要一種不使用這個套件的方法來做到這一點(我仍然可以使用現有的通用套件)

我希望還有另一種可能性。我找到了一個非常好的方式如果您想從右側剪斷繩子,請執行此操作。但我需要從另一邊得到它。

我將給出一個簡短的例子,我的文檔應該是什麼樣子:

\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(界面為埃格格的版本):

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

在此輸入影像描述

相關內容