Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LaTeX: space after every letter

Tags:

latex

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
_______________ 
like image 287
Ethan Leroy Avatar asked Oct 17 '10 00:10

Ethan Leroy


2 Answers

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}
like image 52
godbyk Avatar answered Oct 05 '22 12:10

godbyk


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
like image 21
Joel Berger Avatar answered Oct 05 '22 12:10

Joel Berger