I simply need to truncate a text string, which is just a sequence of characters without whitespace, therefore the linebreak won't work. I just need to decide the length of the string in centimeter, the rest should be cut.
I found the package trimclip which could do it, but I need a way to do it without the usage of this package (I can still use existing, common packages)
I hope there is another possibility. I found a very nice way to do it if you want to cut the string from the right side. But I need it from the other side.
I will give a short example how my document should look like:
\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}
The text for the example above should look in the document like:
veryLongStri
\EDIT
Sorry for this long edit, but I am pretty confused. The truncate
seems to be buggy somehow (dependent from the string) and as you can see I made some other tests. There are some solutions which seem to work properly. I don't know what problem I had before because some versions only worked outside the tabular environments.
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.
Best Answer
Use the
truncate
packageThe package uses a clever stategy based on hyphenation. But a word of warning, if you just insert long nonsense letter sequences it may have a problem finding a breakpoint. Use sensible text and works very well. Do read the docs. Use
texdoc truncate
from the command line, is the quickest way.