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.

I am trying to use \phantom in the context of an algorithm caption. I am using the algorithm and algorithmic packages. \phantom will work properly in normal text in my document, but not in a caption. Is there a workaround to get whitespace into a caption? I have tried things like \hbox and using \includegraphics[width=0.25in]{smallWhiteImage.png} to create some space. None of these have worked yet.

Any help would be great! Please see the code example below.

What I have:

\begin{algorithm}
\caption{ TestAlgorithm \newline
\textbf{SomeInputs} input1, \newline $\cdots$ input2, input3, input4, \newline $\cdots$ input5, input6, input7 \newline
\textbf{AnOutput} output1 }
\label{algo:TestAlgorithm}
\begin{algorithmic}[1]
\STATE $oh yeah!$
\end{algorithmic}
\end{algorithm}

What I want:

\begin{algorithm}
\caption{ TestAlgorithm \newline
\textbf{SomeInputs} input1, \newline \phantom{$\cdots$} input2, input3, input4, \newline \phantom{$\cdots$} input5, input6, input7 \newline
\textbf{AnOutput} output1 }
\label{algo:TestAlgorithm}
\begin{algorithmic}[1]
\STATE $oh yeah!$
\end{algorithmic}
\end{algorithm}
share|improve this question
1  
Welcome to TeX.SE. While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Oct 9 '12 at 21:00
    
Try using \protect\phantom{$\cdots$}. –  Peter Grill Oct 9 '12 at 21:01
    
Very cool! That works perfectly. Thank you for the very prompt response, I appreciate it! –  texuser Oct 9 '12 at 21:02
    
Why do you want to place the inputs and outputs in the caption? If you are interested only in some rules, there are better ways to achieve this. –  Gonzalo Medina Oct 9 '12 at 21:02
1  
Consider \hspace*{2em} or similar for getting a non disappearing space at the beginning of a line. –  egreg Oct 9 '12 at 21:04

2 Answers 2

You can use \protect\phantom{$\cdots$} to be able to use a \phantom as desired:

enter image description here

As per egreg's comments you can also use \hspace*{2em} to specify much space you want to add. You can specify an absolute horizontal space as in \hspace*{1.0cm}, but it is better to use measurements that adjust based on the current font -- 1em is approximately the horizontal width on the letter m. Other useful horizontal spaces you can use include:

enter image description here

There is also a negative thin space, \! which is useful in certain situations.

Notes:

  • If you need more info on horizontal spacing refer to What commands are there for horizontal spacing?.

  • If you really need exactly the space used by $cdots$, you can use \settowidth to measure that size for you:

    \newlength{\WidthOfCdots}\settowidth{\WidthOfCdots}{$\cdots$}
    

    and then using

     \hspace*{\WidthOfCdots}
    

    will produce exactly the desired horizontal space.

Code:

   \documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\newlength{\WidthOfCdots}\settowidth{\WidthOfCdots}{$\cdots$}

\begin{document}
\section{What you have:}
\begin{algorithm}
\caption{ TestAlgorithm \newline
\textbf{SomeInputs} input1, \newline $\cdots$ input2, input3, input4, \newline $\cdots$ input5, input6, input7 \newline
\textbf{AnOutput} output1 }
\label{algo:TestAlgorithm A}
\begin{algorithmic}[1]
\STATE $oh yeah!$
\end{algorithmic}
\end{algorithm}


\section{What you want:}

\begin{algorithm}
\caption{ TestAlgorithm \newline
\textbf{SomeInputs} input1, \newline \protect\phantom{$\cdots$} input2, input3, input4, \newline \protect\phantom{$\cdots$} input5, input6, input7 \newline
\textbf{AnOutput} output1 }
\label{algo:TestAlgorithm}
\begin{algorithmic}[1]
\STATE $oh yeah!$
\end{algorithmic}
\end{algorithm}

\begin{algorithm}
\caption{ TestAlgorithm \newline
\textbf{SomeInputs} input1, \newline \protect\phantom{$\cdots$} input2, input3, input4, \newline \hspace*{\WidthOfCdots} input5, input6, input7 \newline
\textbf{AnOutput} output1 }
\label{algo:TestAlgorithm B}
\begin{algorithmic}[1]
\STATE $oh yeah!$
\end{algorithmic}
\end{algorithm}

Comparison of horizontal spaces:\bigskip

\begin{tabular}{ll}
\verb|\,| & "\," \\
\verb|\thinspace| & "\thinspace" \\
\verb|\enspace| & "\enspace" \\
\verb|\quad| & "\quad" \\
\verb|\qquad| & "\qquad" \\
\end{tabular}
\end{document}
share|improve this answer
    
Of course this is a very convolute method for adding some space at the beginning of a line. Would you like to add some hints on how to do it more efficiently? –  egreg Oct 9 '12 at 21:05
    
@egreg: Added \hspace version, but the \phantom seems easier to me. Or were you thinking of some other method? Of course that is assuming there was some reason the OP wanted exactly that specific width. –  Peter Grill Oct 9 '12 at 21:12
    
From what the OP says, $\cdots$ is just something that occupies some space. I'd say simply \quad or \qquad, which are the traditional ways, or \hspace*{...} if one wants finer control. –  egreg Oct 9 '12 at 21:19
    
Ok, have updated solution. I guess I use \phantom so often that I assumed there was a reason why the space equal to $cdots$ was required... –  Peter Grill Oct 9 '12 at 21:37
    
\settowidth doesn't require calc; the traditional em is the width of (capital) "M". –  egreg Oct 9 '12 at 21:39

A typesetting system like LaTeX surely has methods for producing white spaces in its output.

The traditional lengths used by (human) printers were called "quad" and "double quad"; the name derives from the Italian "quadrato", that is, "square". It was a piece of metal with a flat square surface that could be inserted among the characters to be set, with its side equal to the font size. The analog of the quad space in TeX is called \quad and the double quad is \qquad.

However, \quad and \qquad produce a space that disappears at line breaks, but there are ways to produce a space that doesn't disappear. The general spacing command (in horizontal direction) is \hspace and \quad is equivalent to \hspace{1em}; the *-variant of \hspace is what we need here:

So your input would be

\caption{TestAlgorithm\newline
  \textbf{SomeInputs} input1,\newline
  \hspace*{1em}input2, input3, input4,\newline
  \hspace*{1em}input5, input6, input7\newline
  \textbf{AnOutput} output1}

or with 2em instead of 1em if you prefer a double quad.

The argument to \hspace can be any legal dimension. The em unit is font dependent and it's better to stick with it, if there are no other reasons (in some cases an explicit length such as 3cm or 2in may be required).

share|improve this answer

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.