3

I have a sentence and I have a word that should go under another specific word in the sentence. Is there a way to do this without using the \hspace option (or using it with another unit of measurement)?

example with \hspace{cm}:

\documentclass{article}

\begin{document}

\noindent
This is a sentence\\
\hspace*{1.45cm}word

\end{document}

I'm looking for a way that avoid me to guess the space for each pair sentence, word I have.

  • 1
    Look also at linguistic packages like expex. ctan.org/topic/linguistic – Ulrike Fischer Aug 8 '15 at 16:11
  • What is your actual use case? If it is for linguistic examples, you should definitely use either expex or the glossing macros that are part of gb4e, especially if you need the aligned words to be able two wrap across lines. – Alan Munn Aug 8 '15 at 16:27
4

If you need this a many times in your document, define your own macro. This solution uses the tabular environment which can be embedded anywhere in text or math, as well as creating conventional "tables".

You might consider different formatting, like changing [t]{@{}l@{}} to {@{}c@{}}

\documentclass{article}

\newcommand{\stackwords}[2]{\begin{tabular}[t]{@{}l@{}}#1\\#2\end{tabular}}

\begin{document}
This is a \stackwords{sentence}{word}
\end{document}
  • I'll try all the solution submitted and all of them work. I choose this. Thank you to everyone for the help! – Doc Aug 9 '15 at 7:50
5

You could use a tabbing environment:

\documentclass{article}

\begin{document}

\begin{tabbing}
This is a \= sentence \\
\> word
\end{tabbing}

\end{document}

The result:

enter image description here

3

A 'stupid' way with \hphantom{...}

\documentclass{article}

\begin{document}

\noindent
This is a sentence

\noindent\hphantom{This is a }word


\end{document}

enter image description here

Edit Some kind of automating this:

\documentclass{article}

\usepackage{xstring}
\newcommand{\displaysentence}[3]{%
\StrPosition{#1}{#2}[\somepos]%
\noindent #1

\noindent\hphantom{\StrLeft{#1}{\numexpr\somepos-1}}#3
}

\begin{document}

\noindent
This is a sentence

\noindent\hphantom{This is a }word

\displaysentence{This is a sentence}{sentence}{word}

\displaysentence{This is a very, very, very, very long sentence}{long sentence}{Here it shall be}


\end{document}

enter image description here

I did not check for errors of course, it's most probably not failsafe.

  • My solution will fail if the sentence wraps around :-( – user31729 Aug 8 '15 at 16:30
2

Just for the sake of completeness and because the \widthof command could be useful in other situations: there is yet another way of doing it.

\documentclass{article}
\usepackage{calc}

\begin{document}

\noindent
This is a sentence\\
\rule{\widthof{This is a }}{0pt}word

\end{document}

However, note that this needs an extra package (unless you use calc anyway) and is much less elegant than Gonzalo's or Christian's solutions.

Please note that although it might seem tempting to use \hspace and just give the widthof as an argument, this won't work without further work. (see this answer)

enter image description here

  • The \rule idea however is nice! – user31729 Aug 8 '15 at 15:45
2

For a general solution of lining up words the glossing macros provided by linguistics packages are ideal since they will also automatically wrap lines and keep the words lined up. The ExPex package is very good, although quite complex to use. But the built-in glossing macros of the gb4e or linguex packages will work very well for most things.

See Numbered examples in linguistics that I can refer back to for an overview of these packages.

Here's a simple example using just the cgloss4e package (documentation in the gb4e package.) The way the command works is that it takes two lines, each terminated by \\ and uses spaces to line up each word in each line. Grouping is respected, so in your example, I've grouped the first three words with braces, so that the first line effectively contains 2 "words". Then since the next line has only one word, we add an empty pair of braces as the placeholder for the first word. This correctly lines up "word" with "sentence".

I've added a longer example to show how the lines wrap automatically while retaining the correct word-to-word alignment.

\documentclass{article}
\usepackage{cgloss4e} % documentation is part of gb4e
\begin{document}
\noindent
\gll {This is a} sentence.\\
{} {word}\\

\gll This package also allows very long sentences to wrap properly and can align each word.\\
     Word each align can and properly wrap to sentences long very allows also package this.\\
\end{document}

output of code

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.