4

I am trying to write a document for several people to sign, their names have different numbers of characters. I would like to make them to have the same length by filling blank spaces to them. For example, in the following document

% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass[UTF8]{article}

\usepackage{ctex}

\begin{document}

\pagestyle{plain}

\maketitle

李梅:
\
王晓华:
\
张丽娟:

\end{document}

I would like it to produce

李 梅:
王晓华:
张丽娟:

where the colons are actually aligned. Sorry I can not make them align exactly in the editor.

The most intuitive way to me is writing a random character in the middle the first name and make that extra character white. But it seems cumbersome and not easily generalizable. Is there a better way?

Thank you in advance!

3

An easy way is to use a tabular:

\documentclass{article}

\begin{document}

\noindent
\begin{tabular}{@{}l@{:}}
  First\\
  Second\\
  Third
\end{tabular}

\end{document}

enter image description here

@{} suppresses or replaces space between columns, or before the first and after the last column—as used here.

Using your template with Chinese characters, this gives:

\documentclass[UTF8]{article}
\usepackage{ctex}

\begin{document}

\noindent
\begin{tabular}{@{}l@{:}}
  李梅\\
  王晓华\\
  张丽娟
\end{tabular}

\end{document}

enter image description here

In case you want to add spacing before the colons, you can start the tabular for instance with

\begin{tabular}{@{}l@{\kern 2pt:}}

enter image description here

If you want a second column that takes all remaining space until the right margin, you can use the X column type of tabularx (we tell tabularx to make the X column large enough so that the table fills the whole \linewidth; you can have several X columns if you wish, they will share the remaining space left by the other columns):

\documentclass{article}
\usepackage{tabularx}
\usepackage{lipsum}

\begin{document}

\noindent
\begin{tabularx}{\linewidth}{@{}l@{:}X@{}}
  First  & \lipsum[1][1-2]\\
  Second & \lipsum[2][1-2]\\
  Third  & \lipsum[3][1-2]
\end{tabularx}

\end{document}

enter image description here

You can start the tabularx this way to insert some spacing after the colon:

\begin{tabularx}{\linewidth}{@{}l@{:\enspace}X@{}}

enter image description here

Justifying the first column

Following up on a request in the comments, here is a little hack to get the first column automatically justified without needing to manually insert spaces between the characters. This definitely requires an Unicode-based engine, such as XeTeX or LuaTeX.

\documentclass[UTF8]{article}
\usepackage{ctex}
\usepackage{collcell}

\makeatletter
\newcommand*{\@insertHfills}[1]{%
  \unless\ifx#1\@nil
    #1\hfill \expandafter\@insertHfills
  \fi
}

% Append an \hfill after each token of #1 and set \parfillskip to zero
% after the result has been left in the TeX input stream.
\newcommand*{\insertHfills}[1]{%
  \@insertHfills#1\@nil
  \parfillskip=\z@\relax
}
\makeatother

% Define a special column type derived from l (r or c would do as well)
% that wraps the cell contents as the argument of \insertHfills.
\newcolumntype{E}{>{\collectcell\insertHfills}l<{\endcollectcell}}

\begin{document}

\noindent
\begin{tabular}{@{}E@{:}}
  李梅   \\
  王晓华 \\
  王j华  \\
  张丽娟
\end{tabular}

\end{document}

screenshot

| improve this answer | |
  • Is it possible to have the blank space filled in between the words? – zyy Dec 21 '19 at 23:42
  • Which words? What do you mean by filling in a blank space? I don't understand what you are asking here, please be more precise. – frougon Dec 21 '19 at 23:56
  • @frougon Most Chinese characters are words in their own right, so I assume this is what zyy means, so the aim is to basically justify these rows. My intuition would be to do something like this but for every line, not just the last of the paragraph. I tried to do this but I think LaTeX isn't too happy about "character" spacing of Chinese characters, even if made into their own paragraphs (Usually CJK wants to align into square em boxes). – gnucchi Dec 22 '19 at 0:34
  • @frougon Sorry that I was not clear, so what I was thinking is to add effectively a white space between 李 and 梅,this way, the names would be aligned at both ends. I should have been more clear in the question, sorry about that. – zyy Dec 22 '19 at 1:47
  • 2
    @svenper and zyy: please have a look at Justifying the first column in my updated answer: this implements the request formulated in these comments, if I understood it correctly. – frougon Dec 22 '19 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.