1

I am writing my own class/preamble for LaTeX and need to differ inside if a package is required and therefore loaded based on the document class.

Example: I want to load \RequirePackage{hyperref} for all document classes but beamer, because it's pre loaded from beamer.

Is this somehow possible?

Thank you.

David Carlisle
  • 757,742
Patrick
  • 105
  • 2
    \RequirePackage{hyperref} does not make any harm if the hyperref package is already loaded, therefore you can load it unconditionally. –  Dec 30 '20 at 19:14
  • 1
    Welcome to TeX.SX! You can use \@ifpackageloaded{<name>}{<true>}{<false>} in the .sty file to test if a package has already been loaded. See also Test if a package (or package option) is loaded. But as @PM noted, \RequirePackage loads the package only if it hasn't been loaded yet, and otherwise does nothing. – gernot Dec 30 '20 at 20:13

2 Answers2

1

Thank you for your help. I've read through and I got a solution. Here is an example with beamer and geometry:

\@ifclassloaded{beamer}%
  {
  }%True
  {  \RequirePackage[a4paper]{geometry}
  }%False
\makeatother%

Maybe this will help somebody

Patrick
  • 105
  • One advice: you should get accustomed to always remove whitespaces after { and } or put % sign immediately after those brackets if a line break follows in your sty and tex sources. Otherwise you might end up with horizontal skips in the output DVI or PDF, respectively. – Lupino Sep 23 '22 at 05:55
0

Welcome to TeX StackExchange.

You mentioned you wanted to write your own class with .sty extension, these kinds of files are called packages not classes. I assume you want to create a template to ease and standardize the process of creating documents for the future. If this is your goal, you should create a class, which has .cls extension.

Must-read before writing any kind of class or package is already installed on your system if LaTeX is properly installed. Just go to your terminal and type the following command

$ texdoc clsguide

or simply click here for the same content. I highly recommend getting familiar with texdoc command. Also, The LaTeX Companion and LaTeX: A Document Preparation System are two great resources.

After reading the clsguide, I you will grasp more about your problem and how things work for class and packages. If not, you will be able to express it more explicitly.

Have fun.