# How to horizontally align all characters like a typewriter

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}

\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}


• Can you add a picture of your desired output as well? – darthbith Sep 29 '13 at 5:24

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}

\author{You}

\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}


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}
\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}


• package calc is not used, left-over from a previous state of the source. – user4686 Sep 29 '13 at 7:33
• I know I'm unlikely to use it, but any idea how to fix it if the argument of CenterIt goes over two lines? – Nathanael Farley Sep 30 '13 at 7:00
• This is fantastic. Considering a typewriter would never adjust any line spacing naturally, losing parskips is no big deal. (In fact, my final document will probably have some pretty stringent limits on line spacing for this reason.) – Nathanael Farley Sep 30 '13 at 22:21

One option is to use a two-step process:

1. Use the center environment to identify the location of the text center with respect to the other document elements.

2. Use a \phantom to horizontally align and manually center the contents.

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\newcommand{\vmark}{\mbox{}\rlap{\smash{\color{red}\rule[-3\baselineskip]{.5pt}{4\baselineskip}}}}
\begin{document}
\ttfamily\raggedright

\begin{center}
\vmark 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.

\hrulefill

\bigskip

\leavevmode\phantom{This is a top secret do}\vmark Top Secret Document

\bigskip

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}


After the first center alignment, it is obvious that an appropriate horizontal placement would be somewhere around the c of document.