I cannot remember how to create an invisible character in LaTeX, i.e. I want to put a space that has the width of a particular character, say `M'. I know there is a command for this, and this is a really dumb question, but my Google-fu has failed me.
[Tex/LaTex] How to create an invisible character
macrosspacing
Related Solutions
It would be a bit fragile to do that in TeX: I'd do the replace with a regex in the editor as you suggest. (If you just wanted to make a ~
in text and a normal space in math that would be easier, but having it work in listings and verbatim would be harder)
For example given
\documentclass{article}
\usepackage{amsmath}
\begin{document}
A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
\end{document}
The command M-x addtilde
in emacs produces
\documentclass{article}
\usepackage{amsmath}
\begin{document}
A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a~bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a~bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
\end{document}
where addtilde
is defined by
(defun hidespaceenv (e)
(goto-char (point-min))
(while (re-search-forward
(if (string-equal e "[") "\\\\\\["
(if (string-equal e "$") "\\$"
(concat "\\\\begin{" e "}")))
nil 1)
(while (re-search-forward "\\([a-zA-Z]\\)\\(\\s-+\\)\\|\\(\\\\begin\\)" (save-excursion(re-search-forward
(if (string-equal e "[") "\\\\\\]"
(if (string-equal e "$") "\\$"
(concat "\\\\end{" e "}")))
nil 1) (point)) 1)
(replace-match "\\1SPACE@@\\2\\3@@" t))))
(defun addtilde ()
(interactive)
(mapcar `hidespaceenv (list "verbatim" "align" "equation" "[" "$"))
(goto-char (point-min))
(while (re-search-forward "\\(\\(^\\|\\s-+\\)[a-zA-Z]\\)\\s-+" nil 1)
(replace-match "\\1~"))
(goto-char (point-min))
(while (re-search-forward "SPACE@@\\(\\s-+\\|\\\\begin\\)@@" nil 1)
(replace-match "\\1")))
The classical way is using \@bsphack
and \@esphack
. They are used by \label
or \index
, both supposed to be invisible/empty regarding spacing.
These commands remember, if there is a space before the command. If yes, the second space afterwards is suppressed by \ignorespaces
. The commands are defined in the LaTeX kernel:
\def\@bsphack{%
\relax
\ifhmode
\@savsk\lastskip
\@savsf\spacefactor
\fi}
\def\@esphack{%
\relax
\ifhmode
\spacefactor\@savsf
\ifdim\@savsk>\z@
\ignorespaces
\fi
\fi}
Applied to \todo
:
\documentclass{article}
\makeatletter
\newcommand{\todo}[1]{%
\@bsphack
\@esphack
}
\makeatother
\begin{document}
Test.
\todo{foo}
Test\todo{foo}. Test.
Test\todo{foo}. \todo{foo} Test.
\end{document}
Limitation: The method can fail, if the macros using \@bsphack
and \@esphack
are used one after each other. The second \@bsphack
does not know the state of the previous one. Therefore \ignorespaces
can be suppressed leaving the following space.
Best Answer
You already found the answer, but let me expand a bit. There are three phantom commands. They each take a single argument.
\hphantom
(horizontal phantom) inserts an empty box that has zero height, zero depth, but the width of its argument.\vphantom
(vertical phantom) inserts an empty box that has the height and depth of the argument, but zero width.\phantom
inserts an empty box with the same dimensions (horizontal as well as vertical) as the argument.