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?

10
  • Have a look at the amsmath package. It offers many environments for "math problems" (including splitting equations over several lines). Commented Sep 12, 2017 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
    Commented Sep 12, 2017 at 17:18
  • If you want an automatic line breaking, your second line, without \seqsplit (which is not relevant here anyway), works fine.
    – Bernard
    Commented Sep 12, 2017 at 17:18
  • @magula Maybe add that requirement (automatic line break) to your question. Commented Sep 12, 2017 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
    Commented Sep 12, 2017 at 17:20

2 Answers 2

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}
2
  • You seem to have forgotten some \extrarowheight
    – Bernard
    Commented Sep 12, 2017 at 18:42
  • @Bernard the \hline are just to highlight the table width, not for real output:-) Commented Sep 12, 2017 at 18:45
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

You must log in to answer this question.

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