3

I have a somewhat special problem. I want to create a fixed-width table column (for example by using the p{0.2\textwidth} column-type). But in the column are really (!) long words, containing mostly numbers, brackets, and plus-signs. I want LaTeX to perform linebreaks at the + signs and the ( signs, and the ) signs, if any linebreaks needed.

The intended behavior is, to use the +, (, and ) as if it is a -. It should not insert a hyphen -, if it hyphenate a word, just use the +, (, and ) as new hyphenation signs (or only one of them, if it is to difficult to hyphenate at everyone).

The words are automatically created, and cannot be controlled to wrap by the insert of manual spaces, and the list of the words is really long, so doing all this word wrap by hand would last the rest of the year...

To make this problem not too easy (/irony off), of course it has to be in a special, self-defined column-type (which should center the column on a fixed width... hopefully i did this right... if this is absolutely not possible, it can also be a fixed-width left-aligned column-type)

Hopefully, someone can help me out of this mess. Thanks in advance.

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{tabular}{C{0.8\textwidth}C{0.2\textwidth}}
here is some stuff & itp(itp(itp(itp(itp(15+16)+23)+27)+itp(123+5))+itp(itp(itp(itp(98+3)+6)+27)+89)) \\
\end{tabular}
\end{document}

2 Answers 2

3

I'd use \raggedright, rather than \centering. You can define the +, ( and ) to issue a penalty, so a break can be taken.

\documentclass{article}
\usepackage{array,calc}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1-2\tabcolsep}}
\newcolumntype{M}[1]{>{\raggedright\arraybackslash$\setupspecialmath}p{#1-2\tabcolsep}<{$}}

\newcommand{\setupspecialmath}{%
  \begingroup\lccode`~=`+
  \lowercase{\endgroup\def~}{\mathchar\pluscode\penalty0 }%
  \begingroup\lccode`~=`(
  \lowercase{\endgroup\def~}{\mathchar\oparcode\penalty0 }%
  \begingroup\lccode`~=`)
  \lowercase{\endgroup\def~}{\mathchar\cparcode\penalty0 }%
  \mathcode`+=\string"8000
  \mathcode`)=\string"8000
  \mathcode`(=\string"8000
  \rmmg}
\edef\pluscode{\the\mathcode`+ }
\edef\oparcode{\the\mathcode`( }
\edef\cparcode{\the\mathcode`) }

\AtBeginDocument{\sbox0{$\mathrm{\xdef\rmmg{\fam\the\fam}}$}}

\begin{document}

\noindent
\begin{tabular}{C{0.8\textwidth}M{0.2\textwidth}}
here is some stuff & 
itp(itp(itp(itp(itp(15+16)+23)+27)+itp(123+5))+itp(itp(itp(itp(98+3)+6)+27)+89)) \\
\end{tabular}
\end{document}

enter image description here

(Margins added for clarity, they're not produced by the code above.)

3
  • Thank you very much for your answer! I don't know why, but this does not work, if two columns next to each other have the type M (i have 7 columns or what, and 4 of them have to be the wrapping type... in my ignorance, i thought this is no difference, but obviously it is -.-) edit: it is not the other column, it is using mathmode in the column... this seems to be not possible... damn
    – joooo
    Commented Jun 24, 2013 at 17:56
  • now i got it. do not use mathmode in the header of longtable, then it works perfectly for non-multirows. i really think i should get rid of these multirows... :)
    – joooo
    Commented Jun 24, 2013 at 18:04
  • @joooo Yes, M columns use mathmode. My usual comment about multirow is that I've not yet seen a table using \multirow that can't be improved by removing it. ;-)
    – egreg
    Commented Jun 24, 2013 at 18:09
4

enter image description here

\documentclass{article}
\usepackage{array,url}
\makeatletter
\def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
 \do\(\do\)\do\,\do\?\do\'\do+\do\=\do\#}%
\def\UrlBigBreaks{\do\:\do@url@hyp}%
\def\UrlNoBreaks{\do\[\do\{\do\<}%
\makeatother

\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\noindent
\begin{tabular}{@{}C{0.8\textwidth}@{}C{0.2\textwidth}@{}}
here is some stuff & \url{itp(itp(itp(itp(itp(15+16)+23)+27)+itp(123+5))+itp(itp(itp(itp(98+3)+6)+27)+89))} \\
\end{tabular}
\end{document}

If you would rather not use \url itself, or want different fonts etc, url package lets you define commands with similar behaviour.

Note I also removed the paragraph indent and the inter-column spacing so that your columns of width .8 and .2 \textwidth fit on a line. You might prefer to have the columns narrower than that but restire at least the middle inter-column space (which is removed via @{} in the above)

7
  • hey thank you very much! i didnt think about using the url package. (to be true, is did not use it till know, because everytime i did, my document did not compile... but this is another issue, and as i didnt really need it till now, i just ignored the problem :))
    – joooo
    Commented Jun 24, 2013 at 17:15
  • it works perfectly, if the wrapping does not occur in a multirow (which i forgot in my minimal example). is it possible that this is going to work in a multirow environment?
    – joooo
    Commented Jun 24, 2013 at 17:40
  • @joooo multirow shouldn't make any difference. Commented Jun 24, 2013 at 17:51
  • hm... but it does... the lines `here is & \multirow{2}{*}{\url{itp(itp(itp(itp(itp(15+16)+23)+27)+itp(123+5))+itp(itp(itp(itp(98+3)+6)+27)+89))}} \some stuff & \` does not wrap...
    – joooo
    Commented Jun 24, 2013 at 18:13
  • 1
    @joooo That is nothing to do with this content multirow * is like a c column or an \mbox no line breaking occurs. You want a p column so \multirow{2}{0.2\textwidth}{\url{....}} Commented Jun 24, 2013 at 18:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .