26 January 2010 2 Comments

Use Conditional (\ifdefined) in LaTeX – Tips

Inpendently typeset document fragments

  • Inserting the fragments. Problem: \input does not handle relative paths in the inserted files correctly. Solution: package import, command \subimport{dir/}{file}
  • Hiding the preamble. Problem: package ifthen provides commands for conditional text inclusion, but this package can only be included after the preamble. Solution: use \ifdefined
1
2
3
4
5
6
7
8
9
10
 % main document
 \newcommand{marker}{}

% subdocument
\ifdefined\marker
% things that only appear in compound mode
\else
\documentclass[a4paper]{article}
% things that only appear in stand-alone mode
\fi
  • Chapter title. Problem: Using the fragment independently, one assigns a \title, inside the main document, this title is assigned via \chapter. Solution (the very first line of the subdocument should then be the title, it comes even before ifdefined and documentclass):
1
2
 \newcommand{\title}[1]{\chapter{#1}}
 \subimport{chapter1/}{chapter1
} % file chapter1/chapter1.tex
  • Referring to the current document (“this chapter” vs. “this document”): TODO, can be done in a single location if one uses the marker.

Conditionals

FIXME: The above solution does not work well with Emacs which needs \chapter to construct the table of contents.

1
2
3
4
5
6
7
8
9
10
11
    \ifdefined\foo
      \message{\string\foo\space is defined}%
    \else
      \message{no command \string\foo}%
    \fi
    %
    \ifcsname foo\endcsname
      \message{\string\foo\space is defined}%
    \else
      \message{no command \string\foo}%
    \fi
Tags:
  • pps

    Perhaps you should give a link to the page where this content comes from, since it seems that it is the original source: http://www.pst.ifi.lmu.de/~rauschma/cgi-bin/wik…

    You have a “cite this article link”, but do not provide a citation for others' work.

  • http://www.americanrecordablemedia.com/ CD DVD Printer

    Very nice set of codes. Thanks for sharing this.