Preserve all leading and trailing spaces

spacing

I'm doing this:

\documentclass{article}
\begin{document}
\newcommand\print[1]{[\texttt{#1}]}
\print{  foo    }
\end{document}

I'm getting:

[ foo ]

How can I make it print this:

[  foo    ]

Best Answer

Consider activating \obeyspaces:

enter image description here

\documentclass{article}

\makeatletter
\newcommand\print{%
  \begingroup
  \obeyspaces
  \@print
}
\newcommand{\@print}[1]{%
  [\texttt{#1}]%
  \endgroup
}
\makeatother

\begin{document}

\print{  foo    }

\print{ foo     }

\print{      foo}

\print{foo      }

\end{document}