tags:

views:

68

answers:

2

I want to define a LaTeX command that inserts a space after every letter.

So, If I add

\addSpaces{someText}

the result should be

s o m e T e x t 

How could I achieve this?

Background: I want each letter to be underlined, but the line should be separated between the letters:

s o m e T e x t 
_ _ _ _ _ _ _ _

NOT:
s o m e T e x t
_______________ 
+1  A: 

You can use the soul package for underlining. For single words, you can use the \addspaces macro I wrote below. (The macro will swallow the space between words. A simple workaround is to use \quad to increase the space between the words.)

\documentclass{article}

\usepackage{soul}% provides underlining

\makeatletter% since we're using a macro containing @
\newcommand{\addspaces}[1]{%
  \@tfor\letter:=#1\do{%
    \ul{\letter}\space
  }%
}
\makeatother% resets @ to its original meaning

\begin{document}

% Works fine for single words
\addspaces{Spaces}

% Note that spaces in between words aren't preserved -- use \quad to add extra space.
\addspaces{Spaced\quad and\quad underlined.}

\end{document}
godbyk
A: 

For programmatic manipulation of text I find it much easier to use perltex to define a perl function to do the code and then compile the document. See CTAN here.

Here is a quick and dirty.

\documentclass{article}
\usepackage{perltex}

\perlnewcommand{\ulspace}[1]{
$input = shift;
$input =~ s/(\w)/\\underline\{\1\} /g;
return $input;
}

\begin{document}

\ulspace{Hello World}

\end{document}

Compile with:

perltex --latex=pdflatex myfile.tex
Joel