[Tex/LaTex] Unrecognized commands in chapters using report document class

errorstexstudio

I'm getting 'unrecognized command' errors in my chapter_1.tex file when using the following directory structure (using TeXstudio)

  • Report
    • chapters
      • chapter_1.tex
    • main
      • main.tex

with the following code.

main.tex

\documentclass[a4paper,12pt]{report}

\usepackage{amsmath}

\makeatletter
    \def\input@path{{../chapters/}}
\makeatother

\begin{document}
    \chapter{Test Chapter}
    \label{chap:chapter_1}
    \input{chapter_1}
\end{document}

chapter_1.tex

% !TeX root = ../main/main.tex
\begin{align}
    1+1&=2 \\
    2+2&=4
\end{align}

Here, I get 'unrecognized command' errors with align because chapter_1.tex does not have \usepackage{amsmath}, which I can't include because chapter_1 is essentially within the document. It also does not automatically indent or autocomplete properly.

Everything compiles just fine, but I'd like to know how to get rid of these errors. In my actual files I have many such errors, so it's quite bothersome.


Edit: Found a "fix"!

The problem happens only if you have a fresh TeXstudio session (i.e. if you properly exited each individual file shown in the tabs above the editor). So with a fresh session,

  1. Open the main.tex and the chapter_1.tex files. You should(?) see my problem mentioned in this post.
  2. Now, close the entire TeXstudio session without first exiting the individual files.
  3. Open either .tex file again. The problem should now be fixed in chapter_1.

Notes: If you open another chapter file after step 3, for example chapter_2.tex, the problem will exist for chapter_2.tex until you do steps 2-3 again. Also, if you close main.tex (the file with the packages) and try to exit and restart your TeXstudio session then any chapters that you kept open will have the problem again. main.tex must always be open before exiting the session.

Good enough for me.

Best Answer

Found a "fix"!

The problem happens only if you have a fresh TeXstudio session (i.e. if you properly exited each individual file shown in the tabs above the editor). So with a fresh session,

  1. Open the main.tex and the chapter_1.tex files. You should(?) see my problem mentioned in this post.
  2. Now, close the entire TeXstudio session without first exiting the individual files.
  3. Open either .tex file again. The problem should now be fixed in chapter_1.

Notes: If you open another chapter file after step 3, for example chapter_2.tex, the problem will exist for chapter_2.tex until you do steps 2-3 again. Also, if you close main.tex (the file with the packages) and try to exit and restart your TeXstudio session then any chapters that you kept open will have the problem again. main.tex must always be open before exiting the session.

Good enough for me.