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.

Is there a way to conditionally 'phantom' parts of my document (without typing the same 'phantomed' text twice)?

Currently, I am using the etoolbox package, newtoggle/settoggle, and iftoggle to selectively expand parts of my document.

\usepackage{etoolbox}
\begin{document}
\newtoggle{flag_name}
\settoggle{flag_name}{true}
\iftoggle{flag_name}{\phantom{long_text}}{long_text}
\end{document}

Notice that if long_text is long, then I have two very long text segments, which is undesirable because the .tex file becomes longer and larger with regard to disk size. I was whether it was possible to 'phantom' long_text without necessarily typing long_text twice.

I am trying to do something like this, but I am having difficulty in getting LaTeX to recognize the brace character is part of the .tex file, instead of as a brace character as part of the final document:

\usepackage{etoolbox}
\begin{document}
\newtoggle{flag_name}
\settoggle{flag_name}{true}
\iftoggle{flag_name}
{
\phantom{}\{
}
{}
long_text
\iftoggle{flag_name}
{
\}
}
{}
\end{document}

Logically, I am trying to get the phantom braces around long_text whenever the flag is true. So, long_text will always be outputted, but the phantom will appear around it depending on the truth-hood of the flag.

Thanks.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You can just insert the appropriate macro - either \phantom or \relax (which does nothing):

enter image description here

\documentclass{article}
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\begin{document}
\newtoggle{flag_name}
\settoggle{flag_name}{true}
\iftoggle{flag_name}{\phantom}{\relax}{long text} short text \par
\settoggle{flag_name}{false}
\iftoggle{flag_name}{\phantom}{\relax}{long text} short text
\end{document}

Why does this work? It's because \phantom does not take any arguments. It is an "intermediate" function before the magic happens, so your toggle just inserts the correct macro in the input stream. Make sure that you brace the text after your \iftoggle.

Joseph Wright's comment: Using \relax here leaves in a set of braces, which could affect spacing in math mode. Safer would be to define \newcommand{\firtofone}[1]{#1} and use that (or to \makeatletter and use \@firstofone).

share|improve this answer
    
Thanks. Why does \phantom{long text} short text, with \phantom{long text} at the beginning of the line, not produce blank spaces? However, \noindent\phantom{long text} short text does produce the blank spaces. Also, \underline{\phantom{long text}} short text produces blank spaces. –  jrand Aug 26 '12 at 5:53
    
In response to my earlier comment: apparently, \phantom behaves interestingly when used in combination with \indent and \noindent (separately). This probably has to do with the "create invisible box of dimensions of the argument" behavior of \phantom. –  jrand Aug 26 '12 at 6:16
1  
@Werner Using \relax here leaves in a set of braces, which could affect spacing in math mode. Safer would be to define \newcommand{\firtofone}[1]{#1} and use that (or to \makeatletter and use \@firstofone). –  Joseph Wright Aug 26 '12 at 6:21
    
@JosephWright: I had thought about \@firtstofone, but didn't want to clutter the interface since I wasn't sure of the context. I'll add your suggestion. –  Werner Aug 26 '12 at 14:37
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.