51

I want to share a latex document via git with many other people. Therefore we decided to put all the special sty files, that are not present in everyones latex-installation, into a resources directory. It would be cool, if this dir would be a superdir. of the actual working directory How exactly can I import those style files?

It is important that even the dependencies of those remote styles are resolved with other remote styles.

serv-inc
  • 35,772
  • 9
  • 166
  • 188
wuschelhase
  • 1,084
  • 1
  • 9
  • 17

5 Answers5

39

You can import a style file (mystyle.sty) into your document in two ways:

  1. If you have it in your path or in the same folder as the .tex file, simply include this line in your preamble: \usepackage{mystyle}
  2. If you have it in a different folder, you can access using its full path as \usepackage{/path/to/folder/mystyle}

That said, if you're not sure if the style file is in everyone's installation, simply include it in the same directory and make sure you do git add mystyle.sty and track it along with the rest of your files (although most likely there won't be any changes to it). There is no need for a parent directory. But if you insist on a different directory, see option 2 above.

It would be better if it were in a subdirectory than in a parent directory, as you can still call the file as \usepackage{subdir/mystyle} and be certain that you are invoking your style file. However, if you escape out to the parent directory, you never know if the other users have a similarly named folder that is not part of your package, which can result in errors.

abcd
  • 41,765
  • 7
  • 81
  • 98
  • 5
    Is this also supposed to work with `\documentclass`? I can't seem to load a class file located into a subfolder of my main .tex file. – Waldir Leoncio Jun 12 '16 at 19:37
  • 7
    As [pointed out by David Carlisle](http://tex.stackexchange.com/a/124013/6903), the fact that `\usepackage{/path/to/folder/mystyle}` works is due to lack of error checking. The argument to `\usepackage{}` should be a name not a path, so 2. is not really a good option. – mforbes Jul 26 '16 at 20:43
13

This probably isn't relevant to you any more, but here is another way to do what you want. Set up your git repository like this:

mystyle.sty
project/
    makefile
    project.tex

and put \usepackage{mystyle} in the preamble of project.tex. Compiling project.tex manually won't work, of course, because mystyle.sty is not in the same directory as project.tex.

However, if makefile contains something along the lines of:

project.pdf: mystyle.sty project.tex
    pdflatex project

mystyle.sty: ../mystyle.sty
    cp ../$@ $@

then running make from within the project directory will cause mystyle.sty to be copied to the correct place before project.tex is (this time successfully) compiled.

This way might seem a little bit over the top, but it does combine the best features of other methods.

  1. If several projects in the same repository require mystyle.sty then having a common mystyle.sty sitting above them all makes more sense than having a copy in each project directory; all these copies would have to be maintained.
  2. The compilation is portable, in the sense that if you gave me your copies of mystyle.sty and project.tex then I would (in theory at least) be able to compile manually without needing to modify the files you gave me. For example, I would not have to replace \usepackage{/your/path/mystyle} with \usepackage{/my/path/mystyle}.
Dave Wilding
  • 241
  • 2
  • 5
3

You can use Makefiles as suggested above. Another option is CMake. I didn't test for parent directories.

If you have the following file structure:

    ├── CMakeLists.txt
    ├── cmake
    │   └── UseLATEX.cmake
    ├── img
    │   └── logo.jpg
    ├── lib
    │   └── framed.sty
    └── main.tex
  • you should have CMake installed, instructions on CMake resources

  • UseLATEX.cmake can be downloaded from here

  • then inside the CMakeLists.txt

    ╚═$ cat CMakeLists.txt
    cmake_minimum_required (VERSION 2.6)
    set(PROJECT_NAME_STR myProject)
    project(${PROJECT_NAME_STR})
    
    set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
    include(UseLATEX)
    
    ADD_LATEX_DOCUMENT(main.tex
                       IMAGE_DIRS img
                       DEFAULT_PDF
                       MANGLE_TARGET_NAMES)
    
  • Some example content for main.tex (note the image)

    ╚═$ cat main.tex
    \documentclass{report}
    
    \begin{document}
    
    \begin{center}
      \includegraphics[width=300px]{img/logo.jpg}
    \end{center}
    
    \end{document}
    
  • The lib directory has the *.sty files

  • You can now compile:

    cd /directory/that/has/CMakeLists.txt/
    mkdir build
    cd build
    cmake ..
    make
    
  • you can then view main.pdf which is in the build directory.

lunamystry
  • 321
  • 2
  • 6
  • I like CMake for this, but your example doesn't seem to work and I don't see how it could. The string "lib" doesn't appear in [UseLATEX.cmake](https://cmake.org/Wiki/File:UseLATEX.cmake), or any of the code in your post. How does pdflatex know that's where the *.sty files are? – cgmb Mar 20 '18 at 01:02
  • 1
    It wasn't too hard to fix this. I just manually changed [the style-copying code in UseLATEX.cmake](https://github.com/kmorel/UseLATEX/blob/1c817c70f296efa1074cea02d5c67042078461e9/UseLATEX.cmake#L1885-L1890) to copy files from `${CMAKE_CURRENT_SOURCE_DIR}/lib` rather than `${CMAKE_CURRENT_SOURCE_DIR}`. – cgmb Apr 06 '18 at 23:55
3

When you use TeX distribution that uses kpathsea, you can use the TEXINPUTS environment variable to specify where TeX is looking for files. The variable needs to be used in the following way.

The paths in TEXINPUTS are separated by :. An empty path will include the default search paths, i.e., just the colon. Two consecutive slashes means that the directory and all sub-directories are searched.

Thus, e.g., to build a file document.pdf which uses files in the current directory, all sub-directories of the resources directory and the default directories, you can use the following Makefile.

document.pdf: document.tex
    TEXINPUTS=.:./resources//: pdflatex document.tex

To speed up the filename lookup, you can build a ls-R database using the mktexlsr command.

For all the details on kpathsea take a look at the manual.

H. Rittich
  • 814
  • 7
  • 15
2

You can use latexmk and its facilities

There is a feature documented under Utility subroutines on page 48 here in latexmk which can update TEXINPUTS during a run. If you can consider to use the .latexmkrc file to configure your chain and options, you can add ensure_path() to the file:

Here is an example:

# .latexmkrc
ensure_path('TEXINPUTS', './path/to/something//', '/full/path/to/something/else//')
# [...] Other options goes here.
$pdf_update_method = 3;
$xelatex = 'xelatex -synctex=1 -interaction=nonstopmode -file-line-error %O %S';
$pdf_previewer = 'start "%ProgramFiles%/SumatraPDF/SumatraPDF.exe" %O %S';
$out_dir = 'build/';

Notice the // at the end of a path, This will aid LaTeX to search for files in the specified directory and in all subdirectories.

Please note that while this is an amazing feature, you need to take good care of your naming scheme. If you use the same file name several places, you can run into trouble when importing them with, say \include{somefile}.

Ole Aldric
  • 725
  • 6
  • 22