[Tex/LaTex] How to horizontally align all characters like a typewriter

horizontal alignmenttypewriter

I want to make a document that looks like an old top secret file written on a typewriter. The problem is that when I want to use a center environment, the alignment of the characters is off. Is there a way to make sure that the boxes of all the characters are aligned while still making use of the center environment?

MWE

\documentclass[a4paper]{article}

\title{Your Paper}
\author{You}

\begin{document}
\ttfamily 
\raggedright
\begin{center}
Top Secret Document
\end{center}
This is a top secret document containing secret information about a secret thing which has remained secret for many years. This is a top secret document containing secret information about a secret thing which has remained secret for many years.
\end{document}

example of typewriter center

Best Answer

In response to Nathanael comment, I update my earlier answer with a more powerful method, able to cope with multi-paragraph material, pagebreaks being allowed.

The extra inter-paragraphs vertical skips (\parskip) inside the specially centered material will be lost, though. As this is usually 0pt plus some stretchable glue this effet is not very significant.

\documentclass[a4paper]{article}
\usepackage[paperheight=11cm,paperwidth=12cm,vscale=0.75,hscale=0.7]{geometry}
\usepackage{xcolor}
\usepackage{lipsum}

\title{Your Paper}
\author{You}

% from Werner's answer

\newcommand{\vmark}{\mbox{}\rlap{\smash{\color{red}\rule[-3\baselineskip]{.5pt}{4\baselineskip}}}}

\newcommand{\Vmark}{\mbox{}\rlap{\smash{\color{blue}\rule[-\baselineskip]{.5pt}{3\baselineskip}}}}

\newlength{\dimenA}
\newlength{\dimenB}

% command \CenterIt
\newcommand\CenterIt[1]{\settowidth{\dimenB}{#1}%
\noindent\hspace{\dimexpr((\linewidth-\dimenB)/2/\dimenA)*\dimenA }#1\par}

% and now a *much* fancier tool, multi-paragraph-able, and allowing page breaks.

\makeatletter
\newbox\tmpbox
\newenvironment{ttcentered}{%
  \edef\@tempa
     {\endgroup
      \setbox\z@\vbox{\setbox\tmpbox\vbox{}%
                      \noexpand\centering
                      \begingroup
                      \aftergroup\noexpand\ttcenter@
                      \aftergroup}%
                      \aftergroup\noexpand\ttcenter@post
                      \def\noexpand \@currenvir   {\@currenvir }%
                      \def\noexpand \@currenvline {\on@line }%
     }\@tempa}{}% 

% remove or comment the \Vmark for real use.
\def\ttcenter@{\endgraf
  \loop
        \setbox\z@\lastbox
        \unskip\unpenalty\unskip\unpenalty
  \ifvoid\z@\else
        \setbox\tw@\hbox{\strut
\Vmark 
                         \unhbox\z@}%
  \setbox\tmpbox
         \vbox{\moveright 
                \dimexpr((\linewidth-\wd\tw@)/2/\dimenA)*\dimenA\relax
                \box\tw@\penalty\z@
                \unvbox\tmpbox}%
 \repeat
 \global\setbox\@ne\vbox{\unvbox\tmpbox}}

\def\ttcenter@post{\vspace{\dimexpr\baselineskip-\ht\strutbox-\prevdepth}%
                   \unvbox\@ne
                   \prevdepth\dp\strutbox}

\makeatother

\begin{document}

\ttfamily \settowidth{\dimenA}{x}

\raggedright
\CenterIt{\vmark Top Secret Document}
\CenterIt{\vmark This is a Top Secret Document}
\CenterIt{\vmark This is a truly Top Secret Document}

This is a top secret document containing secret information about a secret thing
which has remained secret for many years. This is a top secret document
containing secret information about a secret thing which has remained secret for
many years.

\begin{center}
  \begin{ttcentered}
    This is a top secret document containing secret information about a
    secret thing which has remained secret for many years. This is a top
    secret document containing secret information about a secret thing
    which has remained secret for many years.

    \lipsum[1]
  \end{ttcentered}
\end{center}

% debugging:
% \showoutput

    We check the vertical spacing induced by the center environment and
    set up some centered text without extra vertical spacing (apart from
    the \string\parskip=\the\parskip):

  \begin{ttcentered}
    This is a top secret document containing secret information about a 
    secret thing which has remained secret for many years. This is a top
    secret document containing secret information about a secret thing
    which has remained secret for many years.
  \end{ttcentered}


Back to normal raggedright (to confirm absence of extra vertical
spacing; apart from \string\parskip=\the\parskip).

%\showoutput
\end{document}

align1

align2

align3


First answer:

A method based on measuring things first. On each line. (\vmark copied from Werner's answer)

\documentclass[a4paper]{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\vmark}{\mbox{}\rlap{\smash{\color{red}\rule[-3\baselineskip]{.5pt}{4\baselineskip}}}}
\usepackage{calc}
\title{Your Paper}
\author{You}

\newlength{\dimenA}
\newlength{\dimenB}

\newcommand\CenterIt[1]{\settowidth{\dimenB}{#1}%
\noindent\hspace{\dimexpr((\linewidth-\dimenB)/2/\dimenA)*\dimenA }#1\par}

\begin{document}
\ttfamily 
\settowidth{\dimenA}{x}

\raggedright



\CenterIt{\vmark Top Secret Document}
\CenterIt{\vmark This is a Top Secret Document}
\CenterIt{\vmark This is a truly Top Secret Document}

This is a top secret document containing secret information about a secret thing which has remained secret for many years. This is a top secret document containing secret information about a secret thing which has remained secret for many years.
\end{document}

horizontal typewriter algnment