How to horizontally align all characters like a typewriter
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}
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}
One option is to use a two-step process:
Use the
center
environment to identify the location of the text center with respect to the other document elements.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
.