Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

My code is this:

\setlength{\parindent}{0ex}
\texttt{
x\hspace{1ex}\hspace{1ex}\hspace{1ex}y
\newline
\hspace{1ex}\hspace{1ex}\hspace{1ex}\hspace{1ex}y
}

What I'd like to see is:

x   y
    y

What I actually see is:

x   y
y

I've tried various other ways of inserting a space. So far, they've all resulted in leading whitespace being ignored.

I've also tried using \verbatim and \alltt but they eat too many of the other commands that I need preserved (not shown in the example above).

I suppose I could (in the script that emits the LaTeX) count the leading spaces in each line and use different \parindent values. I'm looking for something more elegant first.

share|improve this question
3  
We could do with a fuller explanation of the real use case here. –  Joseph Wright Dec 6 '11 at 20:48
    
Your requested alignment can be obtained in a number of ways, including tabular or even tabbing. –  Werner Dec 6 '11 at 20:54
    
Jo seph. good question! I am trying to takedocuments that use a bastardised legacy markup language (similar to bbcode) and create both a HTML/CSS rendition and a LaTeX rendition. The incoming markup is a given, so I can't tweak that to make the job easier. I don't want to lose any of the formatting unless something is actually impossible in LaTeX and/or HTML/CSS. The HTML/CSS side is not a problem for me. My LaTeX skills are less than a week old, so this (and my other questions) demonstrate my failure to have yet fully internalised 1000+ page manuals. –  DavidT Dec 6 '11 at 21:47
add comment

4 Answers

up vote 10 down vote accepted

The other answers here address how to use \hspace at the beginning of the line via the \hspace*.

An alternate way of inserting spaces of an appropriate width is to use \phantom{} which will take up as much space as would be required by the parameter passed to it. This will adapt more easily to cases where the amount of space you are trying to insert is not just an integer multiple of 1ex/1em.

As barbarabeeton: mentions em should be used for horizontal spaces, and ex for vertical ones. A good reference is Which measurement units should one use in LaTeX?.

enter image description here

\documentclass[border=5pt]{standalone}
\begin{document}
\texttt{\noindent
x\hspace{3ex}y
\newline\noindent
\phantom{x}\hspace{3ex}y
}
\end{document}
share|improve this answer
    
Thanks. This fits my usage case brilliantly. Thanks too to the other responders. I can see their approaches will valuable in other circumstances. –  DavidT Dec 6 '11 at 21:49
    
@Peter -- ideally, em should be used for horizontal spaces, and ex for vertical ones. your comment regarding integer multiples still applies, although integers aren't necessary in this context. –  barbara beeton Dec 7 '11 at 14:38
    
@barbarabeeton: Good points. I should have mentioned that, but was just focused on the leading space issue. Thanks for pointing that out. –  Peter Grill Dec 7 '11 at 19:20
add comment

Use the starred version \hspace*.

share|improve this answer
add comment

You need to make some changes: 1) \hspace is ignored at the beginning of a line, so you should use \hspace* instead. 2) ex is the height of the x character; for proper alignment you'll need to use 0.5em which represents the width of a character in monospaced font. 3) There's a spurious blank space in your code.

The following example presents your code with ex, a variant with em, and a tabbing approach that could be also of interest:

\documentclass{article}

\begin{document}

\noindent\texttt{%
x\hspace{0.5ex}\hspace{0.5ex}\hspace{0.5ex}y
\\
\hspace*{0.5ex}\hspace{0.5ex}\hspace{0.5ex}\hspace{0.5ex}y
}

\noindent\texttt{%
x\hspace{0.5em}\hspace{0.5em}\hspace{0.5em}y
\\
\hspace*{0.5em}\hspace{0.5em}\hspace{0.5em}\hspace{0.5em}y
}

{
\ttfamily
\begin{tabbing}
\=\hspace{2em}\=\kill
\>x \>y \\
\>\> y
\end{tabbing}
}

\end{document}

enter image description here

share|improve this answer
add comment

use it this way to prevent additional spaces before x:

\setlength\parindent{0ex}

{\ttfamily
x\hspace{1ex}\hspace{1ex}\hspace{1ex}y
\newline
\hspace*{1ex}\hspace{1ex}\hspace{1ex}\hspace{1ex}y}

Or better

\ttfamily
x\hspace{1ex}\hspace{1ex}\hspace{1ex}y

\hspace*{1ex}\hspace{1ex}\hspace{1ex}\hspace{1ex}y
\normalfont
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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