5

I would like to place several blocks of text in one line, but fixing their positions by distances from the beginning of the line. For example

(1.2pt from beginning) cat (2.1pt from beginning) dog (2.9pt from beginning) bird

\hspace does not work, since it fixes the distances from the end of the last word. Is there a command which fixes this distance from the beginning of the line? If the words are too long or too close to each other, they should overlap.

  • There are several ways how to do that: \rlap, \hbox to, tabbing, tabular, ... In order to give you the best answer, we would need to know some background as what do you want it for. Maybe construct a MWE using \hspace* and then say what should be modified? – yo' Nov 1 '14 at 12:55
4

The tabto package does what you want. If a \tabto requires a leftward tab, it will issue a linefeed, unless you use the \tabto* invocation. Below, I show successive \tabto's with and without the * variant.

Note that a blank line appears between the two versions in the output because the \tabto{1.2pt}cat is a leftward tab relative to the \parindent value (so a linefeed was inserted). If I had added a \noindent prior to the invocation, no blank line would have appeared.

\documentclass{article}
\usepackage{tabto}
\begin{document}
\tabto*{1.2pt}cat
\tabto*{2.1pt}dog
\tabto*{2.9pt}bird

\tabto{1.2pt}cat
\tabto{2.1pt}dog
\tabto{2.9pt}bird
\end{document}

enter image description here

3

With tabbing

\documentclass{article}

\begin{document}
\noindent\vrule height2pt depth 2pt\hrulefill\vrule % to show the line width

\begin{tabbing}
\=\hspace{10pt}\=\hspace{10pt}\=\hspace{10pt}\=\kill
\>\> cat \> dog \> bird \\
\end{tabbing}
\end{document}

enter image description here

I refuse to show the result with too narrow distances.

An environment where you can specify the distances from the left margin and doesn't require the starting \>\>

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment{ftabbing}{m}
 {
  \marioscool_set_distances:n { #1 }
  \begin{tabbing}
  \tl_use:N \l__marioscool_preamble_tl
 }
 {
  \end{tabbing}
 }

\tl_new:N \l__marioscool_preamble_tl
\dim_new:N \l__marioscool_distance_dim

\cs_new_protected:Npn \marioscool_set_distances:n #1
 {
  \tl_clear:N \l__marioscool_preamble_tl
  \dim_zero:N \l__marioscool_distance_dim
  \clist_map_inline:nn { #1 }
   {
    \tl_put_right:Nx \l__marioscool_preamble_tl
     {
      \exp_not:n { \=\hspace } { \dim_eval:n { ##1 - \l__marioscool_distance_dim } }
     }
    \dim_add:Nn \l__marioscool_distance_dim { ##1 }
   }
  \tl_put_right:Nn \l__marioscool_preamble_tl { \=\+\+\kill }
 }
\ExplSyntaxOff

\begin{document}
\noindent\vrule height2pt depth 2pt\hrulefill\vrule % to show the line width

% this one is for comparison
\begin{tabbing}
\=\hspace{10pt}\=\hspace{10pt}\=\hspace{10pt}\=\kill
\>\> cat \> dog \> bird \\
\end{tabbing}

\begin{ftabbing}{10pt,20pt,40pt}
cat \> dog \> bird \\
AAA \> BBB \> CCC \\
\end{ftabbing}

\end{document}

enter image description here

2
\documentclass{article}

\usepackage{picture}

\begin{document}

\noindent
\begin{picture}(0,0)
\put(1.2pt,0){cat}
\put(2.1pt,0){dog}
\put(2.9pt,0){bird}
\end{picture}

\end{document}
  • What is the meaning of the first coordinate (0,0)? Is it the anchor of some box? – Sigur Nov 1 '14 at 13:03
  • @Sigur it's the size of the box for the entire picture (not very big in this case:-) – David Carlisle Nov 1 '14 at 13:13
  • 1
    @Sigur it's standard latex syntax (just used the picture package so could use pt not integer coordinates). See for example tex.stackexchange.com/a/209940/1090 – David Carlisle Nov 1 '14 at 13:14
1

Here is a possible solution. Note that your horizontal spaces are too short. I made an example with other units, just to show better the output.

The black lines are the left and top margins of text area, produce by \usepackage[showframe]{geometry}.

MWE

\documentclass{report}
\usepackage[showframe]{geometry}

\begin{document}

\noindent%
\rlap{\hspace*{1.2pt}cat}%
\rlap{\hspace*{2.1pt}dog}%
\rlap{\hspace*{2.9pt}bird}

\noindent%
\rlap{\hspace*{1.2ex}cat}%
\rlap{\hspace*{2.1ex}dog}%
\rlap{\hspace*{2.9ex}bird}
\end{document}

enter image description here

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.