LaTeX - Adding a varying amount of horizontal spacing to the beginning of each new line in a sequence [closed]
Asked Answered
F

4

6

I'm using LaTeX for an algorithms assignment and I need to show the steps for Horspool's algorithm for string matching similar to what is shown in the textbook. The way it demonstrates the algorithm is showing how the pattern shifts along the text for each failed comparison, with each shift on a new line. The pattern is shown below the text with appropriate horizontal spacing indicating which letters are being compared.

Here is an example of what it would look like with a DNA sequence:

GAGTAATCCTTCACTTCAAGGCCAGTCTTCACATCTCATCAGA
ACATCTCA
 ACATCTCA
  ACATCTCA
    ACATCTCA

I've poked around on several LaTeX references. I tried using \hspace to add the spacing at the beginning of each line, then adding \hfill after the pattern and before creating a newline. I'm not getting any errors, but there is no space being added at the front. The line is being filled correctly.

Is there another way to add the space to the beginning of each line, or another way to format this?

Felloe answered 6/2, 2010 at 9:12 Comment(2)
This question appears to be off-topic because it is about LaTeX.Clothespress
When I asked it almost 4 years ago, there was no LaTeX Stack Exchange. It's fine with me if it needs to be moved.Felloe
G
8

I am fairly sure you want to typeset your examples with a fixed-width font, in which case, you should probably use verbatim environment. Even better, you could use fancyvrb package, which gives you a Verbatim environment, and allows you control over captioning, coloring, font size/shapes, etc. You can even make blanks more visible by showing them:

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\begin{Verbatim}[showspaces=true,fontsize=\small]
GAGTAATCCTTCACTTCAAGGCCAGTCTTCACATCTCATCAGA
ACATCTCA
 ACATCTCA
  ACATCTCA
    ACATCTCA
\end{Verbatim}
\end{document}
Gehlbach answered 6/2, 2010 at 10:13 Comment(1)
This is perfect. Thanks for the recommendation.Felloe
P
28

Just use \hspace*{5mm} (i.e. the starred form) which works fine at the beginning of a line.

Pinko answered 6/2, 2010 at 9:24 Comment(1)
This worked perfectly inside Beamer, which fancyvrb did not.Rebhun
G
8

I am fairly sure you want to typeset your examples with a fixed-width font, in which case, you should probably use verbatim environment. Even better, you could use fancyvrb package, which gives you a Verbatim environment, and allows you control over captioning, coloring, font size/shapes, etc. You can even make blanks more visible by showing them:

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}
\begin{Verbatim}[showspaces=true,fontsize=\small]
GAGTAATCCTTCACTTCAAGGCCAGTCTTCACATCTCATCAGA
ACATCTCA
 ACATCTCA
  ACATCTCA
    ACATCTCA
\end{Verbatim}
\end{document}
Gehlbach answered 6/2, 2010 at 10:13 Comment(1)
This is perfect. Thanks for the recommendation.Felloe
K
7

For this, I always add an "empty" object to the beginning of the line, e.g.

${}$\hspace{5em}Foo

it's not beautiful, but serves the purpose.

Kolyma answered 6/2, 2010 at 9:16 Comment(0)
K
1

I would simply use the listings package and do the indention with spaces or tabs. I suppose that you are using the typewriter font anyway to get letters with a non-relative width.

Kyungkyushu answered 6/2, 2010 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.