LoginSignup
2
4

More than 5 years have passed since last update.

[TeX] input{file}のパス指定

Posted at

\input{file}

$\mathrm{\TeX}$で論文とかそこそこ長い文章を書くときは\input{file}を使ってファイルを入れ子にする。
でもこのfile名、基本は相対パスで書くけれど、実はコンパイル実行時の作業ディレクトリからの相対パスだったりします。

document.tex
\documentclass[12pt]{article}

    \input{config.tex}

\begin{document}

    \input{introduction.tex}

\end{document}

普通はこうする。

normal
    $ latex document.tex

    -------
 |__ _  parent/  
    |__ _tex-files/      # 作業ディレクトリ
       |
       |_  document.tex
       |_  config.tex
       |_  introduction.tex

何も問題ない。
じゃあ次はどうなるか

on-parent
    $ latex _tex-files/document.tex

    -------
 |__ _  parent/       # 作業ディレクトリ 
    |__ _tex-files/
       |
       |_  document.tex
       |_  config.tex
       |_  introduction.tex

本文もファイルたちの相対位置も変わっていないけれど、これだとエラーが出る。
作業ディレクトリの下にconfig.texもintroduction.texも見つからないからだ。

じゃあこれはどうだ

on-parent
    $ latex ../dir1/document.tex

    -------
 |__ _  parent/
    |__ dir1/
    |  |
    |  |_  document.tex
    |
    |__ dir2/       # 作業ディレクトリ 
       |
       |_  config.tex
       |_  introduction.tex

想像の通り問題なく通る。


どこからコンパイルしても正しく動作するようにするには、
やはり絶対パスを書くしかない。
しかし絶対パスで書いてしまうと可搬性がなくなってしまう。
とりあえず思いつく解決策としては、どこからコンパイルしようともlatexコマンドは規定の作業ディレクトリで起動するようなスクリプトを組み、
latexではなくそのスクリプトを通してコンパイルする、といった手法だろうか。

現在計画中

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4