TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write a macro in which I can split a string with a linking character (like a hyphen) into different words. So, I have something like \macro{one-two-three} which I would like to split into three different words. Similarly, \macro{four-five} should print "four five" (please note the space).

A restriction is that I don't want to load any package to keep the tex-file as small as possible. So I don't want to use xstring or substr or anything else.

How do I write a macro which returns me the separate words?

I'm new here, so my apologies if I didn't explain my problem clearly.

share|improve this question
Welcome to TeX.SX! With "return" you mean that writing \macro{four-five} LaTeX should print "four five"? – egreg Oct 29 '12 at 14:44
Welcome to TeX.sx! A tip: You can use backticks ` to mark your inline code. – Corentin Oct 29 '12 at 14:44
Yes indeed. The space inbetween is mandatory for me. – Peter Oct 29 '12 at 14:45
If you want to keep your tex-file small loading a suitable package is the way to go. I doubt that a handmade definition can be shorter than \usepackage{...}. – Ulrike Fischer Oct 29 '12 at 14:46

This will work without any package; but it would be much easier with xstring or xparse:








enter image description here

Version with xstring:

\newcommand{\splitlist}[1]{\StrSubstitute{#1}{-}{ }}

Version with xparse:


Advanced version with xparse:

\NewDocumentCommand{\splitlist}{ m }
  \tl_set:Nn \l_tmpa_tl { #1 }
  \tl_replace_all:Nnn \l_tmpa_tl { - } { ~ }

Alternative way suggested by Herbert:

  \lowercase{\def~}{ }%

However this definition disallows \splitlist being in the argument to another command.

share|improve this answer
if we make - active and then let-\space maybe another idea – Herbert Oct 29 '12 at 16:06
@Herbert It indeed can be. – egreg Oct 29 '12 at 16:09
Wow, thanks for this elaborate answer, it works perfectly! – Peter Oct 29 '12 at 16:29

What about something like







share|improve this answer
This is also a nice solution, and easier to understand for me as a newbee. Thanks a lot! – Peter Oct 29 '12 at 16:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.