2

I'd like to let LaTeX split a formula that's inside a fixed-width table. The formula is a group theoretical word (something like $a_1b_3a_5b_3^{-1}$).

It should automatically split it somewhere between the elements so that it fits in the table. I thought seqsplit might do the trick, but in the following code example:

\documentclass{article}
\usepackage{seqsplit}


\begin{document}

\begin{tabular}{p{4cm}}
 \seqsplit{$a_1b_3a_5b_3^{-1}$}\\
 \seqsplit{$a_1$ $b_3$ $a_5$ $b_3^{-1}$}\\
 \seqsplit{abcd}
\end{tabular}

\end{document}

Both variants with the formula just raise lots of errors of the form Missing { inserted \seqsplit{$a_1b_3a_5b_3^{-1}$}, whereas the abcd-sample works.

The second example actually wouldn't give the desired result anyway, because there should be no spaces between the elements.

Does anyone have a solution to this?

magula
  • 409
  • Have a look at the amsmath package. It offers many environments for "math problems" (including splitting equations over several lines). – Dr. Manuel Kuehner Sep 12 '17 at 17:10
  • Thanks, but every amsmath environment I can think of or find requires explicit linebreaks (\\\), i.e. it doesn't split automatically, as far as I know. – magula Sep 12 '17 at 17:18
  • If you want an automatic line breaking, your second line, without \seqsplit (which is not relevant here anyway), works fine. – Bernard Sep 12 '17 at 17:18
  • @magula Maybe add that requirement (automatic line break) to your question. – Dr. Manuel Kuehner Sep 12 '17 at 17:19
  • @Bernard You're right, that works, but it prints spaces between the elements, which is of course not intended. (I should have been more clear there.) Is there any way of prohibiting that? – magula Sep 12 '17 at 17:20
  • @magula In general, I recommend to split manually. This improves the quality and doesn't take much time. – Dr. Manuel Kuehner Sep 12 '17 at 17:26
  • @Dr.ManuelKuehner Yes, now I see that I didn't actually make that clear at all, sorry about that. I've specified it now, as well as the fact that there should be no spaces between the elements, which the second example (with which I just unsuccessfully tried to locate the reason seqsplit doesn't work) obfuscated a bit. – magula Sep 12 '17 at 17:28
  • @Dr.ManuelKuehner I would normally split formulas manually, but in this case I print several computer-generated long tables with lots of those words. A few of them are too long to fit on the page, so I'd like them to get split automatically, which would be quite annoying to do by hand (especially if I change the content of that table at some point). – magula Sep 12 '17 at 17:31
  • To remove spaces, change a bit the table preamble: >{\raggedright}p{4cm}. You'll get a more or less important white space at the end of the lines, but not between the elements. If it's OK for you, you also can write >{\centering}p{4cm}. – Bernard Sep 12 '17 at 17:31
  • I assume that I should load the package array for that (without that, it doesn't compile)? I still get spaces between the elements (even though there somehow seems to be a longer space in the first line without that change). – magula Sep 12 '17 at 17:46

2 Answers2

5

enter image description here

\documentclass{article}
\usepackage{array}


\begin{document}

\begin{tabular}{>{\spaceskip=1sp\relax\raggedright\arraybackslash}p{2cm}}
\hline
 $a_1$ $b_3$ $a_5$ $b_3^{-1}$ $a_1$ $b_3$ $a_5$ $b_3^{-1}$\\
\hline
\end{tabular}

\end{document}
David Carlisle
  • 757,742
3

You can input the long word in a single sequence, and add \hfil\allowbreak\hfilneg before each letter.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\longword}{m}
 {
  \magula_longword:n { #1 }
 }

\tl_new:N \l_magula_longword_tl

\cs_new_protected:Nn \magula_longword:n
  \tl_set:Nn \l_magula_longword_tl { #1 }
  \regex_replace_all:nnN
   { ([a-z]) }
   { \c{hfil}\c{allowbreak}\c{hfilneg} \1 }
   \l_magula_longword_tl
  $\l_magula_longword_tl$
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{p{4cm}}
 \longword{
   a_1b_3a_5b_3^{-1}a_1b_3a_5b_3^{-1}abcd
   a_1b_3a_5b_3^{-1}a_1b_3a_5b_3^{-1}
   a_1b_3a_5b_3^{-1}a_1b_3a_5b_3^{-1}
   a_1b_3a_5b_3^{-1}a_1b_3a_5b_3^{-1}
   a_1b_3a_5b_3^{-1}a_1b_3a_5b_3^{-1}abcd
 }
\end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712