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.

why when i comment \usepackage{geometry} i get error?

\documentclass{book}

\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {                               % i the package was loaded 
    \newcommand*{\qw}{\itshape}  % this is enabled, else is not enabled.                                                                                               
 }{}
\makeatother

\begin{document}
\qw hallo
\end{document}
share|improve this question
    
See section 96.3 in source2e.pdf. –  egreg Sep 5 '12 at 10:46
    
The question has changed from its original form. –  StrongBad Sep 5 '12 at 11:10
    
... and the edit makes the question quite confusing, as the code it contains works just fine. –  Josh O'Brien May 2 '13 at 16:52
add comment

3 Answers 3

up vote 4 down vote accepted

LaTeX defines several conditional macros, with a syntax very different from the primitive

\if?<condition><true>\else<false>\fi

where \if? denotes any of the primitive conditionals. The conditionals defined by LaTeX have the form

\@ifsomething{<arg>}{<true>}{<false>}

or

\@ifsomething{<arg1>}{<arg2>}{<true>}{<false>}

Conditionals of the first kind are \@ifundefined and \@ifpackageloaded; among the second kind are \@ifpackagelater and \@ifpackagewith. The motivation for the distinction is that in the second kind we need something more to check a condition (the package name and an option name, for \@ifpackagewith).

The <true> and <false> code are executed respectively when the condition is satisfied or not. So

\@ifpackageloaded{geometry}
  {\newcommand\qw{\itshape}}
  {\newcommand\qw{\bfseries}}

is the answer. Either argument can be empty, of course.

You find an example of \@ifpackageloaded and some more information in Italian in this book (p. 78 for the main problem)

Note There is also \@ifdefinable<control sequence>{<true>} that has no "false" branch; it will execute the code <true> if the <control sequence> is undefined and raise an error otherwise (ignoring the <true> code).

share|improve this answer
    
I would to ask you if is better leave empty the argument (if I want to make nothing) or put into it the command \relax. Also in the case of the \ifthenelse{\boolean{@nameofboolean}}{}{} command, beacuse for examples, I have see into the classicthesis.sty many \relax as argument of that command. Thanks. –  FormlessCloud Sep 11 '12 at 22:06
    
classicthesis.sty is not an example of good LaTeX programming, in my opinion. An empty argument is way better than filling it with \relax. –  egreg Sep 11 '12 at 22:08
    
I have understand. Is possible made also somthing like: \if@a5paper? –  FormlessCloud Sep 12 '12 at 21:37
    
@FormlessCloud That control sequence name is illegal; but there are examples in the .sty file of \ifthenelse{\boolean{@a5paper}}{<true>}{<false>} –  egreg Sep 12 '12 at 21:40
    
Yes, I have seen those, but I wanted to make that condition without the ifthen package. –  FormlessCloud Sep 12 '12 at 21:47
show 4 more comments

The else part should go in the second argument:

\documentclass{book}

%\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {% if the package was loaded
  \newcommand*{\qw}{\itshape}%
 }
 {%else:
  \newcommand*{\qw}{\bfseries}%
 }
\makeatother

\begin{document}
\qw hallo
\end{document}
share|improve this answer
    
and if i want to do nothing if is not loaded the package? –  FormlessCloud Sep 5 '12 at 10:45
    
@FormlessCloud Leave the "false" argument empty. –  egreg Sep 5 '12 at 10:48
2  
@egreg, then \qw won't be defined. @FormlessCloud, what do you want \qw to be if you do not load geometry? –  StrongBad Sep 5 '12 at 10:52
    
if I not load the package I would that nothing is do –  FormlessCloud Sep 5 '12 at 10:55
add comment

I think what you want is

\documentclass{book}

\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {
    \newcommand*{\qw}{\itshape}  % this is enabled
 }{
    \newcommand*{\qw}{\bfseries} % this is enabled?
}
\makeatother

\begin{document}
\qw hallo
\end{document}

The \@ifpackageloaded takes two arguments: what to do if the package is loaded or not. Your \if doesn't really do anything.

Edit: The question seems to have changed

Now the problem is that if the package is not loaded, then the macro \qw does not get defined. Something like this might be what you want

\documentclass{book}

\usepackage{geometry}

\makeatletter
\@ifpackageloaded{geometry}
 {
    \newcommand*{\qw}{\itshape}  % this is enabled
 }{
    \newcommand*{\qw}{} % this is enabled?
}
\makeatother

\begin{document}
\qw hallo
\end{document}
share|improve this answer
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.