43

Is there any way to remove spaces from in between the words in a text? Something like

\RemoveSpaces{This is   my    sentence.}

which will be converted to

Thisismysentence.

2
  • Why would you want to do this?
    – User 17670
    Commented Mar 6, 2013 at 15:48
  • 1
    As a deterrent against copy and pasting the real text from the resulting searchable PDF file.
    – Reza
    Commented Mar 6, 2013 at 20:18

4 Answers 4

53
\makeatletter
\def\RemoveSpaces#1{\zap@space#1 \@empty}
\makeatother
1
  • I didn't believe it, but ... it works!
    – yo'
    Commented Dec 18, 2012 at 14:53
33

A different approach, that ensures correct hyphenation and line breaks between words is

\newcommand{\RemoveSpaces}[1]{%
  \begingroup
  \spaceskip=1sp
  \xspaceskip=1sp
  #1%
  \endgroup}

This reduces the interword space to the minimum possible positive value (that's not visible to the human eye), but still paragraphs will be built as usual. Complete example:

\documentclass{article}
\usepackage[latin]{babel}
\usepackage{ragged2e}
\usepackage{lipsum}
\def\RemoveSpaces#1{%
  \begingroup
  \spaceskip1sp
  \xspaceskip1sp
  #1%
  \endgroup}

\begin{document}
\parbox{5cm}{\RaggedRight
\RemoveSpaces{\lipsum[2]}
}
\end{document}

enter image description here

16

a solution with lualatex for all spaces between words in a paragraph. Kerning and discretionaries are not affected.

\documentclass[english]{article}
\usepackage{babel,libertine,luacode,blindtext}
\begin{luacode*}
function rSpace(List)
 for line in node.traverse_id(0,List) do -- go through all lines
   for s in node.traverse_id(10,line.head) do node.remove(line.head,s) end 
 end 
 return List
end
\end{luacode*}
\begin{document}
\blindtext \par
\directlua{luatexbase.add_to_callback("post_linebreak_filter",rSpace,"rSpace")}
\blindtext \par

\end{document}

enter image description here

1

This can handle macros in the argument, but it will not remove spaces from groups within the argument (as in argument lists within the argument to \RemoveSpaces)

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listofitems}
\newcommand\RemoveSpaces[1]{\setsepchar{ }\readlist\myarg{#1}\foreachitem\x\in\myarg{\x}}
\begin{document}
\RemoveSpaces{This is   my    sentence.}

\RemoveSpaces{This is   my  \detokenize{\  textit {italic}}  sentence.}
\end{document}

enter image description here

Here is a version that can reach into the groups within the argument. However, the restriction that goes along with this ability is that the argument must now be expandable.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listofitems}
\newcommand\RemoveSpaces[1]%
  {\edef\tmp{#1}\setsepchar{ }\readlist\myarg{\tmp}\foreachitem\x\in\myarg{\x}}
\begin{document}
\RemoveSpaces{This is   my    sentence.}

\RemoveSpaces{This is   my  \detokenize{\  textit {italic}}  sentence.}
\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 .