# Substitute a space to a string to force tabular cell separation

I'd like to construct tables using macros to align syllables with other text. For that task, I defined a table with no separation between columns. This, however led to words being stuck together if connected with an ampersand.

Substituting with xstring and StrSubstitute does not work for macros like \hspace or \phantom, and \, strangely produces an error. Naturally simple written spaces are ignored by the tabular environment. How could the space be inserted?

Here's a dummy example. First, a version with no substitutions. Then one with \,\,. Finally, the wanted result. For me, this example compiles with errors about inserting a missing } in Overleaf. But it does produce the correct result.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\RequirePackage{xstring}

\begin{document}

\begin{tabular}{@{}*{8}{l@{}}}
Wo-&ah we're & half way there, && Wo-&oah & livin' on a prayer \\
{\StrSubstitute{Wo-&ah we're & half way there, && Wo-&oah & livin' on a prayer}{& }{&\,\,}} \\
\end{tabular}

Wo-ah we're half way there, Wo-oah livin' on a prayer

\end{document}

Here's what it looks like:

• As a general rule of thumb: Do not look at the 'output' if you recieve an error message. – leandriis Mar 20 at 20:31
• @leandriis Fair enough, but it did substitute something nevertheless. – Felix Mar 20 at 20:33

Save the string after the substitution and use it; you need a small \expandafter trickery and braces to hide the & from the alignment; with \expandafter we expand \temp before the group ends.

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{xstring}

\begin{document}

\begin{tabular}{@{}*{8}{l@{}}}
Wo-&ah we're & half way there, && Wo-&oah & livin' on a prayer \\
\noexpandarg
{\StrSubstitute{Wo-&ah we're & half way there, && Wo-&oah & livin' on a prayer}
{& }{&\,\,}[\temp]\expandafter}\temp \\
\end{tabular}

Wo-ah we're half way there, Wo-oah livin' on a prayer

\end{document}

• Neat, thanks! Also, is the double \, a proper typing space length? I just aligned it by eye. – Felix Mar 20 at 20:44
• @Felix Since I have no idea what your aim is, so I just answered the question. – egreg Mar 20 at 21:00