[Tex/LaTex] How to vertically align two or more random places of a document

spacingvertical alignment

I would like to find a way to horizontally align any two or more random places of my document (but probably on the same page or not too far from each other). It's a bit like the second column of a tabular, but:

  1. the "rows" can be separated by any amount of whatever. (The tabular can be splitted between the rows.)
  2. the points to be aligned can't always be seen as belonging to a row or cell of a tabular, because they can appear in the middle of a normal paragraph without the rest of the paragraph being affected by this alignment.

How to obtain that all bullets be aligned in the following examples (replacing minuses with white space)?

(1) This sentence is missing X. ———∙ What is this sentence missing?

Some comment/paragraph(s) in between.

(2) X is missing a subject. ————-∙ What/Who is missing a subject?

Some comment/paragraph(s) in between.

(3) My friends invited X to the party. —∙ Who did my friends invite to the party?

Some comment/paragraph(s) in between.

(4) ——————————-∙ Who did my friends invite?

And alike in the following paragraphs (replacing minuses with blank space)?

This paragraph horizontally jumps——-∙ at some point for some
reason. The rest of the text can span other the next lines.

This paragraph wants its jumps to land —∙ exactly at the same
absolute horizontal position as the jump from the above paragraph (not quite perfectly executed here).

How to behave when the position is reached before jumping ∙ is undefined but could be defined by jumping to the next line and aligning there.

The jump can also happen in the second line of the paragraph, so that it looks more like ——————————-∙ this jump.

What (I think) I need is a mecanism to remember some horizontal position (for example 30pt from the left margin) (something like a label remembering neither the last produced number, nor the page it is in, but the absolute horizontal position it has-maybe a tikz node?), then recover it later in the document and use it to horizontally position something at this absolute position (30pt from the left margin).

One specific situation where I need this is with linguistic examples that are built parallel but appear at different parts of the document (see above). Espacially if they are on the same page, it would be good visual semantics to show their belonging together through this alignment. There, the point 2 above doesn't hold, so I used remembering a tabular preamble with tabu in the MWE at the end.

One problem with this solution is that any further left part of an example that is longer than the left part of the first example gets broken. I need to manually add some \hspace at the end of the first cell. But it's something I'm ok with, because I seldom have to align more than a few examples.

  This sentence misses X. \hspace*{4em} & What does this sentence miss?\\

And the second problem is that using a tabular is not always an option as shown in my second case above. See the end of the MWE for that second problem, solved with manually ajusting all \hspace commands:

\documentclass{article}
\usepackage{expex}
\usepackage{tabu}

\begin{document}

\ex \begin{tabu}[t]{X[-1]@{\hspace{1em}}X}\savetabu{mypreamble}
  This sentence misses X. & What does this sentence miss?\\
\end{tabu}
\xe

Comments in between.

\ex \begin{tabu}{\usetabu{mypreamble}}
  X misses a subject. & Who/what misses a subject?\\
\end{tabu}
\xe

Another comment.

\ex \begin{tabu}{\usetabu{mypreamble}}
  My friends invited X to the party. & Who did my Friends invite to the party?\\
\end{tabu}
\xe

This paragraph horizontally jumps \hspace*{3em} \textbullet\ at some 
point for some reason. The rest of the text can span other the next lines.

This paragraph wants its jumps to land \hspace*{.5em} \textbullet\ 
exactly at the same absolute horizontal position as the jump from the 
above paragraph (not quite perfectly executed here).

How to behave when the position is reached before jumping \textbullet\ is undefined.

The jump can also happen in the second line of the paragraph, so that it 
looks like \hspace*{16em} \textbullet\ this jump. 

\end{document}

How can I achieve the case in the examples in an even more automated fashion?

How can I achieve this at all within paragraphs?

bullets to be aligned through examples and paragraphs

Best Answer

The following example puts the question in a \hbox to the right. The width of the box is measured beforehand using the longest question:

\documentclass{article}
\usepackage[
  hmargin=1.5in,
]{geometry}
\usepackage{enumerate}

\newdimen\QuestionLength
\newcommand*{\QuestionFormat}[1]{\textbullet~#1}%
\settowidth{\QuestionLength}{%
  \QuestionFormat{Who did my friends invite to the party?}%
}
\newcommand*{\question}[1]{%
  \leavevmode
  \unskip
  \hspace*{1em plus 1fill}\hbox to \QuestionLength{%
    \QuestionFormat{#1}\hfill
  }%
  \par
}

\begin{document}
\begin{enumerate}[(1)]
\item
  This sentence is missing X.
  \question{What is this sentence missing?}

  Some comment/paragraph(s) in between.

\item
  X is missing a subject.
  \question{What/Who is missing a subject?}

  Some comment/paragraph(s) in between.

\item
  My friends invited X to the party.
  \question{Who did my friends invite to the party?}

  Some comment/paragraph(s) in between.

\item
  \question{Who did my friends invite?}
\end{enumerate}
\end{document}

Result

Version with fixes space to the left

The width of the largest text on the left is put in \LeftPartLength. Then the width of the question is calculated by using the current line width minus the length of the left part and some separation space. An error is thrown, if there is not enough place for the question.

\documentclass{article}
\usepackage[
  hmargin=1.5in,
]{geometry}
\usepackage{enumerate}

\newcommand*{\QuestionFormat}[1]{\textbullet~#1}%
\newdimen\QuestionLength
\newdimen\LeftPartLength
\newdimen\MiddleSep
\setlength{\MiddleSep}{1em}
\settowidth{\QuestionLength}{%
  \QuestionFormat{Who did my friends invite to the party?}%
}
\settowidth{\LeftPartLength}{%
   Some comment/paragraph(s) in between.%
}

\newcommand*{\question}[1]{%
  \leavevmode
  \unskip
  \begingroup
    \dimen0=\dimexpr\linewidth - \LeftPartLength - \MiddleSep\relax
    \ifdim\dimen0<\QuestionLength
      \errmessage{Question is large by %
        \the\dimexpr\QuestionLength-\dimen0\relax.}%
    \fi
    \hspace*{\MiddleSep plus 1fill}%
    \hbox to \dimen0{%
      \QuestionFormat{#1}\hfill
    }%
  \endgroup
  \par
}

\begin{document}
\begin{enumerate}[(1)]
\item
  This sentence is missing X.
  \question{What is this sentence missing?}

  Some comment/paragraph(s) in between.

\item
  X is missing a subject.
  \question{What/Who is missing a subject?}

  Some comment/paragraph(s) in between.

\item
  My friends invited X to the party.
  \question{Who did my friends invite to the party?}

  Some comment/paragraph(s) in between.

\item
  \question{Who did my friends invite?}
\end{enumerate}
\end{document}

Result