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

5 Answers 5

up vote 12 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

Use the starred version \hspace*.

share|improve this answer

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

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

This past week, I have been working on a script that converts some peculiar HTML to LaTeX in order to generate PDF. This HTML contains code samples not in PRE but in P with class assignments and a CSS that makes it look as one would expect a code sample to look. However, being in a P and not a PRE, the code samples include markup for emphasis using color and bold. I find that can process the code samples to escape the usual problem characters, such as underscores... even spaces. The embedded formatting is also easy to convert. The result is not very readable LaTeX, but that is OK because the LaTeX is being used simply as a stepping stone to PDF. Nobody is editing the LaTeX directly.

My biggest problem was with lines of code that started with spaces, which LaTeX eats. (I found this question because it was my problem.) Using verbatim does not work because of the embedded formatting. I also could not get verbatim or even Verbatim to work inside tables, and there are many tables that contain code snippets with embedded formatting.

After much banging on things, I found that, per line, I could do the following:

Plain text plain text plain text plain text plain text plain text plain text.\newline {\tt{{\color{white}{}}\ \ \ some code \textit{here}}}\newline {\tt{{\color{white}{}}\ \ \ some \textbf{more} code \textit{here}}}\newline Plain text plain text plain text plain text plain text plain text plain text.

That was rather a lot to type, and managing all the curly braces was tricky, even in the script that did the conversion work, so I created an environment and applied that environment to each line... a script was doing the work, so that is not as much typing as it would be to do manually.

\newenvironment{codeLine} % {\bgroup\tt\bgroup{\color{white}{}}} {\egroup\egroup\newline}

With that, I could simplify the above to:

Plain text plain text plain text plain text plain text plain text plain text.\newline \begin{codeLine}\ \ \ some code \textit{here}\end{codeLine} \begin{codeLine}\ \ \ some \textbf{more} code \textit{here}\end{codeLine} Plain text plain text plain text plain text plain text plain text plain text.

Compiled output of the above.

I am not sure why it works, but I found it when I thought of entering some white text as the first character of the line. That would work visually, but it would mess up "cut and paste" of the code examples, so I omitted the initial character and it still prevented the gobble up of initial spaces. A simple {} did not work (not for me, at least).

Apologies if this is bad style. I am not really a LaTeX expert. Mostly I just keep trying things until something seems to work... at least for a while. Wiser heads might be able to correct this or warn against it if it is working simply on a fluke that might disappear in the next LaTeX compiler release.

Also, the only package required for this would be the color package, \usepackage{xcolor}. It is possible that \color was not what was absolutely required. Other formatting work at the start of the line might work, but I do not know that.

share|improve this answer
    
Also, I notice now that this behaves a little differently in tables than in paragraph text. So, you might need a \newline or \\ at the end of the code sample. An empty line adds a lot of white space. That might be due to the peculiarities of my particular set of packages. –  Hína Kemenduro yesterday

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.