646

I know how to define a package or a class, but how do I make it available (to myself) at all times?

Say I have the package file commonstuff.sty (or myprettyclass.cls), that I want to be able to include in any .tex file I create on my computer. Where do I put it? Do I need to run some installer on it, or is it enough to keep it in a certain folder?

Note: I know I can just place it in the same folder as the .tex file, but that's not what I'm after here. I'm looking for a way to "install" the package centrally on my computer.

8
  • 3
    Is there a way to place it in an arbitrary directory and point to it? (for portability)
    – IljaBek
    Commented Mar 2, 2014 at 11:41
  • 4
    @troyaner: If you want portability of your documents, you could ship your own packages with the file - *tex will find them if they're in the same folder as the document. If you want to be able to actually install to a custom directory, you can just run mktexlsr and point to that directory, e.g. sudo mktexlsr /path/to/your/texmf. Commented Mar 2, 2014 at 17:17
  • the first solution is the one I was asking for. In case of beamer environment \usetheme{ABC} tries to expand to beamerthemeABC.sty and does not accept paths. This is my usage case for pointing to a directory... not sure if it's a completely different case compared to the OP's, but other posts redirect here
    – IljaBek
    Commented Mar 2, 2014 at 19:18
  • @troyaner: Have you tried putting beamerthemeABC.sty in the same folder as your document? Commented Mar 2, 2014 at 21:02
  • 1
    @troyaner: An alternative approach would be to use some kind of build system in which you can specify a source directory and a build directory. That way, you could have your *.tex and *.sty files in different places, and let the build system handle the finding of them for you. Commented Mar 4, 2014 at 7:59

13 Answers 13

410

You could create a folder below your TeX home directory and put your .sty file therein. Use this command at the command prompt to find out where:

kpsewhich -var-value=TEXMFHOME

On my computer it shows

C:/Users/stefan/texmf

but it might also be ~/texmf/ on a Linux or Unix computer.

Following the TeX directory structure, you should place your file in a subdirectory like ~/texmf/tex/latex/commonstuff/, according to Arthur's comment below. This has the advantage that it is not necessary to update the package database as TeX searches your personal texmf tree directly. If there is an ls-R file in your home texmf tree you can safely delete it as TeX will not use it anyway. (Note: this assumes your personal tree is on a local file system: users with remotely-mounted home folders may still need to hash.)

Regarding MiKTeX, have a look at the section "Installing sty or cls files" in the answer to the question How can I manually install a package on MikTex (Windows).

You can then verify what file will be used with:

kpsewhich filename.sty

This will show the path to the file picked up by the TeX implementation.

22
  • 51
    If he puts his .sty file directly in ~/texmf it just won't work! He needs to respect the TDS structure. Commented Aug 5, 2010 at 14:11
  • 16
    On Linux, so long as you've put the file within the TEXMFHOME tree (in a suitable place) then you simply run texhash on that directory. For example, if TEXMFHOME points to /home/astacey/texmf then texhash /home/astacey/texmf will do, no need for sudo. Commented Aug 5, 2010 at 14:30
  • 23
    Just for completeness: The default location of the home TEXMF tree on Mac OS X is ~/Library/texmf.
    – Philipp
    Commented Aug 5, 2010 at 15:14
  • 1
    @Philip: Not if you use TeX Live. For TeX Live it's always ~/texmf. Commented Aug 5, 2010 at 15:17
  • 6
    @Arthur: No. I have TeX Live 2009 on OS X 10.5 and the default location is, as Philipp has said, ~/Library/texmf. Commented Aug 5, 2010 at 18:30
240

All of the other answers cover things quite well, but I thought a slightly different version might be helpful.

You have to put it in the 'right' place and second you need to update the database TeX uses to find files. The place to put the file depends on your operating system. Assuming you have a standard installation, this will probably be:

  • Windows 10 (and miktex)
    C:\Users\<user name>\Appdata\Local\MikTex\<number>\tex\latex\local\
  • Windows Vista/7 C:\Users\<user name>\texmf\tex\latex\local\
  • Windows XP C:\Documents and Settings\<user name>\texmf\tex\latex\local\
  • Linux ~/texmf/tex/latex/local/
  • Mac OS X /Users/<user name>/Library/texmf/tex/latex/local/

Note: the local/ folder might not exist; in this case, go ahead and create it.

A few of notes on that. First, on Windows the 'Users' part of the location is language dependent. Second, I've represented your home/user folder as '<user name>': this will obviously be dependent on your system. The folder I've indicated may well not exist: you may just have the texmf part, bits within that or nothing at all. The file location is important, and although you could just put your file inside texmf/tex/latex is is usual to keep things organised by package. The local folder is reserved for stuff installed on individual machines.

13
  • 2
    @Juan. Depends on your TeX installation. For recent TeX Live's that is true, but as I understand it older ones do need texhash running.
    – Joseph Wright
    Commented Jan 26, 2011 at 10:52
  • I don't think you've ever needed a database for TeX Live with TEXMFHOME. I've never needed it on Mac, starting around 2002. Admittedly, that was before TL was available for Mac, so gwtex etc. I've never seen the point of using ~/texmf/tex/latex/local/ rather than ~/texmf/tex/latex/<package name>.
    – cfr
    Commented Sep 10, 2017 at 21:35
  • @cfr If you have ii on a network share under some circumstances it is, or at least was, necessary.
    – Joseph Wright
    Commented Sep 11, 2017 at 5:58
  • 1
    For me, the Windows 10 + miktex path is C:\Users\<user name>\AppData\Local\Programs\MiKTeX 2.9\tex\latex\
    – iago
    Commented Nov 8, 2019 at 12:09
  • 2
    Thanks for the answer, why does kpsewhich -var-value=TEXMFHOME not give the trailing tex/latex/local/ part?
    – zyy
    Commented Aug 15, 2021 at 23:39
65

As already mentioned by Arthur, .sty or .cls files must be in some subdirectory of tex\latex which can be in any directory of any drive.

See the following screenshot, it shows how the path to my packages and classes is.

enter image description here

I will explain two cases, registering to MikTeX and registering to TeX Live.

Registering to MikTeX

Open Settings (Admin), select Roots tab, and add my path E:\A\My LaTeX\MyLibrary.

Note that the trailing \tex\latex\misc must not be included !

enter image description here

Afterwards, select General tab and press Refresh FNDB button.

enter image description here

If you cant find MiKTeX Options (Admin) you may have a newer installation that instead uses MiKTex Console. For this version, simply go to the Packages tab and click the database icon.

Registering to TeX Live

See How to register my own packages or classes in a separate drive to TeX Live installation?

1
  • 3
    I would like to inform that in the versions of MikTeX that includes the "MikTeX Console" (after 2018-02-26) the refresh has been replaced to Menu Tasks -> Refresh file name database.
    – FHZ
    Commented Feb 6, 2020 at 2:15
50

The .sty files need to be put it some subdirectory of tex/latex in a texmf tree. Those trees have a rather fixed structure, but their location may vary according to your OS and TeX distribution. If you use Unix (including Mac OS) and TeX Live, there's by default a hierarchy

/usr/local/texlive/2009/texmf
/usr/local/texlive/2009/texmf-dist
/usr/local/texlive/texmf-local

as well as other directories. You want to put it in the texmf-local directory, not in the other directories that are managed by the TeX Live installer; when you upgrade TeX Live, your package may be overwritten. The texmf-local hierarchy, as the names hints, is for local packages managed by you; it won't be touched by updates (which is also why it's directly under the texlive directory, and not texlive/2009, for instance).

You may also choose to create a directory called texmf in your home directory, and the package will be visible only to you, not the other users on your computer.

On Windows with MiKTeX, I believe the directory for local installs is C:\Localtexmf by default.

In any case and whatever your distribution, you have to place the .sty file inside .../texmf-local/tex/latex and not directly in the root directory; it's just won't work otherwise.

Then, if you've installed the package in your home directory, you have nothing to do. If you've put in texmf-local, on the other hand, you need to run the utility mktexlsr (or texhash, it's an alias).

3
  • 7
    I second Arthur's recommendation to use /usr/local/texlive/texmf-local/tex/latex/, if on a Unix system. Commented Aug 5, 2010 at 16:30
  • 1
    For me it was /usr/share/texmf/ on ubuntu 20.04 Commented Feb 1, 2021 at 2:50
  • Last two paragraphs worked perfectly on Windows 10.
    – Mew
    Commented Oct 8, 2022 at 21:19
50

On Linux, you can pick any directory you like, and then add a line like

export TEXINPUTS=".:~/latex:"

to your .bashrc file (you can add multiple directories, separated by colons. I think you do need the '.' in the beginning). There may be something similar in Windows. I find this easier, since it separates "my" stuff from the texmf tree. Probably not great if you're writing a big package, but handy for some small things you just want to share between all your projects.

10
  • 6
    @Neil: No, you don't need the dot in the variable, it's there by default. You do need the trailing colon, though, or the system directories won't be searched at all. Commented Aug 5, 2010 at 15:10
  • 7
    This is not the best solution. There is already a standard user-specific TEXMF tree (~/texmf on Linux), see Stefan Kottwitz's answer.
    – Philipp
    Commented Aug 5, 2010 at 15:13
  • 12
    @Philipp: On my Mac OS X system, the user-specific TEXMF tree is inside ~/Library, which contains random applications' preferences, and other stuff that I haven't put there. It doesn't feel like "my" directory, and I won't remember to look there when I'm (say) copying my preferences to another computer. I really do prefer having my own .sty files in a directory I remember and regularly backup, and using TEXINPUTS. So I will repeat my opinion from the comment I hastily deleted: this is the best solution as far as I can see. Commented Aug 5, 2010 at 20:36
  • 12
    @Philipp: If you have a good memory or use only one computer, I'm sure ~/Library is fine. But I for instance have all my config files (bash, emacs, etc.) and some other stuff in one directory that I keep synced between several computers running Mac OS X, Linux, and Solaris. Obviously, having to update files at several different paths separately on the different computers each time is impractical—and as I said, when I reinstall OS or copy preferences, I'm very likely to forget ~/Library. Nothing I've seen here convinces me this answer is not the best solution… but whatever works for you. :-) Commented Aug 10, 2010 at 3:15
  • 9
    +1 on this answer for project-specific sty directories export TEXINPUTS=".:./sty:. So, in /home/me/my_thesis folder, it will search for custom stys in /home/me/my_thesis/sty. (Put that TEXINPUTS binding just inside your per-project makefile). Commented Feb 25, 2014 at 13:13
16

Using texlive on Ubuntu, the *.sty files are located at

/usr/share/texmf-texlive/tex/latex/

or

/usr/share/texmf-dist/tex/latex/

Adding a new style foo.sty usually means

  1. create folder foo

    sudo mkdir /usr/share/texmf-texlive/tex/latex/foo

  2. copy the file foo.sty there

    sudo cp foo.sty /usr/share/texmf-texlive/tex/latex/foo/

  3. run texhash

    sudo texhash

Works for me with texlive 2009-15 on Xubuntu 12.04LTS

4
  • 6
    Note that Stefan's answer (the one with the most upvotes) describes a much easier way to do this on Linux. For LaTeX: create a folder structure that mimics your TeX-tree: mkdir -p ~/texmf/tex/latex/foo; and for bibtex: mkdir -p ~/texmf/bib/{bib,bst}. Put foo.sty in foo, and put custom bst files in bst. Note that this is not a 'system-wide' location, but one personal to you; but it is easier to deal with (no su needed), and doesn't need texhash because it is always searched automatically.
    – jon
    Commented Nov 8, 2013 at 14:09
  • 6
    Note that user39700's answer is wrong even for system wide installation. The files should be installing into the local tree, not the system one managed by the package manager. That is not to say it will not work. It will. But it is not the way it should be done for a Linux distro.
    – cfr
    Commented Jan 21, 2014 at 3:08
  • 2
    Note that for bibtex, you need to have the following: mkdir -p ~/texmf/bibtex/{bib,bst} and NOT mkdir -p ~/texmf/bib/{bib,bst} as mentioned by jon.
    – aatishk
    Commented Nov 23, 2015 at 11:36
  • Using Ubuntu 20.04 I had to sudo mkdir /usr/share/texmf/tex/latex/foo/ and the rest was the same (with the said directory, obviously)
    – Yair Daon
    Commented Jul 20, 2020 at 6:21
12

The other answers provided to this question all suggest to put your .sty files in the folder /texmf/ in your user profile folder (although there doesn't seem to be any agreement on what exactly you need to do after that).

This will, however, not work if you (like me) are using a portable LaTeX program, because then there won't be any such /texmf/ folder on your computer.

An alternative solution is therefore to put your .sty files (e.g. foo.sty) in any folder you'd like (e.g. C:/foo/), and then simply specify the path where you keep your .sty file when you load it in the preamble:

\usepackage{C:/foo/foo}

To avoid errors or warning messages, be sure to give the path also in your .sty file:

\ProvidesPackage{C:/foo/foo}

To maintain full portability of your LaTeX program, you can put your .sty files in a LaTeX subfolder, e.g. C:/texlive/foo/.

This way you can keep all your .sty files in one single place on your computer, and include the same path with \usepackage in all your .tex documents.

15
  • 3
    But this is not at all portable. Already another Windows user can have put the individual package in another folder, other operating systems do not even know paths starting with C:.
    – Speravir
    Commented Apr 30, 2013 at 20:27
  • 2
    The way you apparently understand and answer the question it had to be closed as “too localized”. Happily other users answered for general use. This said if you would add some strong warning in your answer it would be a lot better.
    – Speravir
    Commented Apr 30, 2013 at 21:30
  • 9
    The argument to \usepackage is a pagckage name not a file path. If the packages that you include this way have a\ProvidesPackage line (as packages should have) then the use described here will produce a warning that the name used is incorrect. Unfortunately (due to compatibility with LaTeX2.09) ProvidesPackage is not mandatory and supplying a path does not give an error or warning in that case, however that is a lack of error checking by the system, it does not mean that this is supported syntax. Commented May 5, 2013 at 9:28
  • 4
    @Sverre oh yes exactly, but that means your package can not be installed in any other location, which makes it a rather odd package. the name in the ProvidesPackage and the name in the usepackage should be the same. any relationship to any path on the file system is an accident of the implementation. Commented May 5, 2013 at 10:46
  • 3
    the standard way to do that is to put your local files in ~/texmf so they are in the standard search path or any other directory of your choice so long as you add it to the TEXINPUTS path. Commented May 5, 2013 at 10:51
12

I've just found this in the MiKTeX documentation. Just FYI:

6.2. Which is the best directory to keep .sty files where MiKTeX can find them? MiKTeX Package Manager takes care of the proper installation of well known LaTeX packages.

If (for some reason) you have to install a package by hand, then you should copy the input files into the directory tex\latex\misc relative to a registered root directory. For example:

Create the root directory C:\Local TeX Files.

Create the sub-directory tex\latex\misc and copy your input files (*.sty, *.cls) here.

Register the root directory (C:\Local TeX Files) in MiKTeX Options.

For more information, read the section Integrating Local Additions in the MiKTeX manual.

1
  • 2
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format Commented Aug 9, 2013 at 2:22
8

For me, using MAC OS X with standard MacTeX, the above answer were only partially helpful. I couldn't find the directory ~/Library/texmf.

I did the following. Assume that you want to add mystyle.sty to your TeX. Create the directory ~/Library/texmf/tex/latex and place mystyle.sty in it. Afterwards invoke texhash ~/Library/texmf/.

This worked for me.

4
  • 3
    You don't need to run texhash for files added in ~/Library/texmf, they will be automatically found. Commented Jan 26, 2011 at 10:28
  • 1
    @Juan: will they be found without any action from my side?
    – Dror
    Commented Jan 27, 2011 at 12:31
  • 1
    yes, see the comments on the accepted answer about TeX Live (which is what MacTeX installs). Commented Jan 27, 2011 at 12:39
  • 2
    Your suggestions were not working until I changed my class name from something-else.cls to somethingelse.cls. Apparently the hyphen was causing a problem. No such problem with my style file with or without a hyphen... Having fixed the name, your suggestions work.
    – PatrickT
    Commented May 22, 2017 at 15:47
7

To avoid tex distribution or operating system specific requirements on where to put stuff and what to run to get it to be found I use a GNUmakefile rule of the following form:

%.sty : ../%.sty
  cp $^ $@

Each subdirectory for which I build latex code references this rule, resulting in the .sty file to be copied locally to that directory before pdflatex is invoked (using suitable dependency rules). See for example, notes/make.rules and notes/phy452/GNUmakefile in my physicsplay github repo.

This works well for me on Linux (ubuntu + TexLive) and Windows (cygwin + MikTex).

3
  • 1
    And what if I compile TeX files all around my disk space, and not in any nicely organized structure? Like: papers in Dropbox, talks here, Copy-editor job there, etc. Am I supposed to create a makefile for all my >100 projects a year?
    – yo'
    Commented May 1, 2014 at 20:39
  • 1
    Would you really be using personal .sty files in so many ad-hoc locations? Even so, there isn't anything that would prevent such a method from being used even from many locations (since the makefiles in each location can be trivial, including just an include of the make rules). Commented May 1, 2014 at 21:34
  • 2
    For one, I use my own beamer style, and the class of the journal I work for. That already makes the process way to complicated. Especially since TEXINPUTS, ~/texmf and texmf-local exist...
    – yo'
    Commented May 1, 2014 at 21:37
7

I am using a quick fix to this issue since I work mainly on a USB stick and don't want to touch to the tex folders the computers I am using.

Indeed, you can specify backward directories to \usepackage, then you don't have to know the exact full path but only the relative path from your .tex to your .sty file.

An example: given a portable hard disk D, I have in D:/latex/files/ my .tex files and in D:/latex/packages/ some packages I want to use, then you can write \usepackage{../packages/mypack.sty}

Not sure if this works on windows however

5
  • 3
    Hi and welcome, i did some additional code markup. You can have a look at our starter guide to familiarize yourself further with our format.
    – Johannes_B
    Commented Nov 26, 2014 at 13:35
  • 1
    This (using ../) will most likely not work because of security restrictions. And it's much better to move these files in a separate texmf tree. Commented Nov 26, 2014 at 13:39
  • 2
    Welcome to TeX.SX! Sure, this approach works to some extent, but I don't see any addition with respect to Sverre's answer. Commented Nov 26, 2014 at 13:39
  • 1
    @ClaudioFiandrino the main point was that you don't need the full path, meaning you can copy and paste your folder everywhere and it still works Commented Nov 26, 2014 at 14:53
  • 1
    How do you handle .cls files?
    – PatrickT
    Commented May 22, 2017 at 15:28
4

For Windows (10) users on Miktex (Console 4.2):

  1. Open Miktex Console (4.2) > Settings > Directories and observe the informational links FAQ: Which is the best directory to keep my .sty files? and KB: TEXMF root directories. We'll follow the advice in those links.

     MikTex (Console 4.2) > Settings > Directories

  2. Anywhere you like: create a TEXMF root directory, with a structure suitable for your custom latex packages (.sty files). Add your package. E.g. C:\Users\John\Documents\Sda\Code\WritingReadingTech\Markup\Latex\Libraries\texmf\tex\latex\MyPackage\MyPackage.sty

  3. Miktex Console (4.2) > Settings > Directories > [+]. Select the TEXMF root directory. E.g. C:\Users\John\Documents\Sda\Code\WritingReadingTech\Markup\Latex\Libraries\texmf. Click [Select Folder]. Observe this folder as in the picture above with "Purposes: Generic", "Attributes: User"

  4. In your latex (.tex) source files, existing anywhere on your computer, add a reference to your custom package with \usepackage{MyPackage}. Build/compile your latex source file. Everything should be as desired.

I found it unnecessary to run Miktex Console (4.2) > Tasks > Refresh File Name Database.

1

This worked for me (Makefile):

KEY := myrepo
TEX := $(shell kpsewhich -var-value=TEXMFHOME)
PWD := $(shell pwd)

link:
  ln -s "$(PWD)" "$(TEX)/$(KEY)"
.PHONY: link

Then:

make link

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .