如何像打字机一样水平对齐所有字符

如何像打字机一样水平对齐所有字符

我想制作一份看起来像用打字机写的旧绝密文件的文档。问题是,当我想使用环境时center,字符的对齐方式会关闭。有没有办法确保所有字符的框都对齐,同时仍然使用环境center

平均能量损失

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

打字机中心的例子

答案1

回应拿但业评论,我用一种更强大的方法更新我之前的答案,能够处理多段材料,允许分页。

不过,特别居中的材料中的额外段落间垂直跳跃(\parskip)将会丢失。因为这通常是0pt加上一些可拉伸胶水,所以这种影响并不明显。

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

对齐1

对齐2

对齐3


第一个答案:

一种基于首先测量事物的方法。在每一行上。(\vmark复制自沃纳的答案)

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

水平打字机对齐

答案2

一个选择是采用两步流程:

  1. 使用center环境来识别文本中心相对于其他文档元素的位置。

  2. 使用\phantom水平对齐并手动居中内容。

在此处输入图片描述

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

第一次center对齐后,很明显合适的水平位置应该在 附近的某个c地方document

相关内容