8

I have a lot of text in table. I want to wrap it with in 2cm. In the last column, I have [1][2][3].....[89][100] but the column width is only 2 cm. It is supposed be wrapped by itself.

enter image description here

How can I implement that?

6
  • 1
    I'm not sure I fully understand your table's setup. Have you tried p{2cm} as the specification of the final column?
    – Mico
    Jun 26, 2012 at 4:05
  • I'd like the see a working example of the problem. I suspect that p{<width>} will not be enough because in the table shown there are no spaces in the content that would act as potential line breaks. Jun 26, 2012 at 4:10
  • I am using p{2cm} but there is no space but still i need to wrap it. I just kept space [1] [2] [3] [4] [5] [6] [7] but still i canot wrap it. My Table definition is here cat("\\centering","\n") cat("\\rowcolors{2}{rowcol}{white}","\n") cat("\\begin{tabular}{>{\\rule{0pt}{.5cm}}>{\\arraybackslash}p{2.25 cm}>{\\arraybackslash}p{2.25cm}c>{\\arraybackslash}p{8cm}c>{\\arraybackslash}p{2cm}c}","\n")
    – manish
    Jun 26, 2012 at 4:15
  • @manish Please add info to the question (with proper formatting).
    – Caramdir
    Jun 26, 2012 at 4:20
  • Above is sweave code. Below is latex code for table definition. \rowcolors{2}{rowcol}{white} \begin{tabular}{>{\rule{0pt}{.5cm}}>{\arraybackslash}p{2.25 cm}>{\arraybackslash}p{2.25cm}c>{\arraybackslash}p{8cm}c>{\arraybackslash}p{2cm}‌ ​c}
    – manish
    Jun 26, 2012 at 4:34

2 Answers 2

5

I think probably the brackets ought to be in math mode, and that gives the advantage of being able to use mathcode "8000 to insert an active definition for the closing bracket.

\documentclass{article}

\usepackage{array}

\edef\savedbrack{\mathchar\the\mathcode`\] }

\begingroup
\uccode`\~`\]
\uppercase{\endgroup
\def\breakablebracket{%
\mathcode`\]"8000
\def~{\savedbrack\penalty 50 }}}

\begin{document}


\begin{tabular}{|>{\raggedright\arraybackslash\breakablebracket$}p{2.0cm}<{$}|}
[1][7][3][8][5][9][7][8][9][89][100]
\end{tabular}

\end{document}

table with wrapped contents

4
  • Above solution works but i can not use alternate row color with P. Is there any way to use alternate color for table with wrap text using breakpoints.
    – manish
    Jun 28, 2012 at 7:09
  • \begin{mdframed}[roundcorner=10pt,leftmargin=1, rightmargin=1, linecolor=yellow,outerlinewidth=.5, innerleftmargin=10,innerrightmargin=10, innertopmargin=10,innerbottommargin=10] \begin{tabular}{P{2.25 cm}P{2.25 cm}P{2.25 cm}P{7 cm}P{2.25 cm}} \rowcolors{3}{yellow}{white} col1 & col2 & col3 & col4 & col5\\hline GSTERR&GD234 &TAT & XYZ &[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22] \\ \hline GSWWQEE&RT1234 &AAC & ABC &[1][2][3][4][5][6][7][8][9][10][11][12][13][14][15][16][17][18][19][20][21][22] \\ \hline \end{tabular} \end{mdframed} \end{document}
    – manish
    Jun 28, 2012 at 7:13
  • In the above i am implementing alternate coloring with breakpoints but cannot do it. Is there any solution for it?
    – manish
    Jun 28, 2012 at 7:19
  • I can't see why row colouring and the breaking of text within the cell should interfere. You didn't include the code to show what package you are using but most of them seem to use my colortbl in the end to do the row colours and that should work for p columns. Jun 28, 2012 at 8:24
13

In this case, using the p{2.0cm} column type is necessary, but not sufficient as there is not a natural break point. This is not related to being in a table, even in normal text, having using content such as [1][1][3][2][5][3][7][8][9][89][100] does not have a natural break point.

So, using just the p{2.0cm} column:

\begin{tabular}{p{2.0cm}}
    [1][7][3][8][5][9][7][8][9][89][100]
\end{tabular}

produces:

enter image description here


Manual Solution:

Here is a solution passing the contents to the ``\AddBreakableChars{}macro, which allows for a discretionary line breaks following a]`. So with:

\begin{tabular}{p{2.0cm}}
    \AddBreakableChars{[1][7][3][8][5][9][7][8][9][89][100]}
\end{tabular}

we obtain the desired result:

enter image description here

References:


Automated Solution:

If you include the \usepackage{collcell} in the preamble, then you can define a new column type that automatically passes each entry in this column through the \AddBreakableChars macro via:

\newcolumntype{P}[1]{>{\collectcell\AddBreakableChars}p{#1}<{\endcollectcell}}

and using a P{<width>} column type as in:

\begin{tabular}{P{2.0cm}}
    [1][10][3][11][5][12][7][8][9][89][100]
\end{tabular}

which yields the desired results as above.


Code:

\documentclass{article}
\usepackage{hyphenat}
\usepackage{xstring}
\usepackage{forloop}
\usepackage{collcell}

\newsavebox\MyBreakChar%
\sbox\MyBreakChar{}% char to display the break after non char
\newsavebox\MySpaceBreakChar%
\sbox\MySpaceBreakChar{-}% char to display the break after space
\makeatletter%
\newcommand*{\BreakableChar}[1][\MyBreakChar]{%
  \leavevmode%
  \prw@zbreak%
  \discretionary{\usebox#1}{}{}%
  \prw@zbreak%
}%

\newcounter{index}%
\newcommand{\AddBreakableChars}[1]{%
  \StrLen{#1 }[\stringLength]%
  \forloop[1]{index}{1}{\value{index}<\stringLength}{%
    \StrChar{#1}{\value{index}}[\currentLetter]%
    \IfStrEq{\currentLetter}{]}
        {\currentLetter\BreakableChar[\MyBreakChar]}%
        {\currentLetter}%
  }%
}%

\newcolumntype{P}[1]{>{\collectcell\AddBreakableChars}p{#1}<{\endcollectcell}}


\begin{document}
\textbf{Manual Solution:}\par
\begin{tabular}{p{2.0cm}}
    \AddBreakableChars{[1][7][3][8][5][9][7][8][9][89][100]}
\end{tabular}

\bigskip
\textbf{Automatic Solution:}\par
\begin{tabular}{P{2.0cm}}
    [1][2][3][4][5][6][7][8][9][89][100]
\end{tabular}
\end{document}
1

You must log in to answer this question.

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