[Tex/LaTex] Windows MikTeX can’t find the .sty file

miktexpackage-writingpackages

I posted a question like this earlier but I'm re-asking because I didn't ask properly before.

I created a .sty file (preamble.sty) to help clean up my preamble but I'm having trouble actually using the packages in my document.

Other questions I have followed (in order) without any luck:

Create a local texmf tree in MiKTeX
My file path to the .sty file is C:/Users/me/texmf/TeX/LaTeX/PreambleStuff. I used the MikTeX GUI to add it as a root in the "Settings," then made sure I clicked "Refresh FNDB"

Where do I place my own .sty or .cls files, to make them available to all my .tex files?
I used the kpsewhich -var-value=TEXMFHOME command line and the command prompt window did not show any file path, just displayed a new C:\Users\me> prompt. Does this mean that I don't have a texmf home directory? I also used the command kpsewhich preamble.sty which shows the correct file path to the file (see file path above).

https://docs.miktex.org/manual/texfeatures.html#includedirectory
I typedlatex --include-directory=C:/Users/me/texmf/TeX/LaTeX/PreambleStuff preamble.sty and the command window shows ! LaTeX Error: \usepackage before \documentclass. See the LaTeX manual or LaTeX Companion for explanation.

The last part raises questions of whether or not I actually wrote my .sty file correctly, but I am mostly concerned with getting MikTeX to find the file and include it properly in each document. What else can I do or what am I doing wrong?

Best Answer

Your local TEXMF tree should not be a subfolder of MiKTeX\texmfs , although it can be a sibling tree as in the screenshot. The key is it ends texmf not texmf\tex\lat...\…

enter image description here

Ideally it should be in an area where you have no user control issues and a common suggestion is C:\users\yourname\mytexmf as similar to ~/mytexmf of Linux. However due to the variety of user names it is better to use something like c:\miktexdata\mytexmf avoiding spaces, punctuation or accented characters.

Once you are sure it looks ok, go to Tasks and Refresh file name database. Exit the console and check MiKTeX is not running in the background. Re-enter MiKTeX console and check the setting is still the same.

IF you are not running console you can do the same from the command line using

initexmf --user-roots=c:\mytexmf --update-fndb=c:\mytexmf --update-fndb

or as suggested in another thread by Ulrike Fischer

initexmf --register-root=c:\mytexmf

If your still having problems, on a portable install that setting may be stored in a single miktexstartup.ini file, check its contents and there is not a second file with that name. In a basic user install it is stored in the registry under Computer\HKCU\Software\MiKTeX.org\MiKTeX\2.9\Core. If your confident with searching you could check the entry is identical.

To test it works download a well tested additional package such as mwe.tds.zip and install that (unzip the contents so included folders doc source and tex are included in your personal texmf as mytexmf/tex/ mytexmf/doc mytexmf/source)

Ensure you Update file name database either in console or via command line

Prepare a simple MWE file

\documentclass{article}
\usepackage{mwe}
\begin{document}
\blindtext
\begin{figure}
\includegraphics[width=.48\linewidth ]{example-image-a}
\includegraphics[width=.48\linewidth ]{example-image-b}
\caption{MWE to demonstrate how to place to images side-by-side }
\end{figure}
\blindtext
\end{document}

it should run and you will see a reference to C:/mytexmf\tex\latex\mwe\mwe.sty thus proving the personal texmf is set correctly for a .sty NOTE: MiKTeX uses / for the stored absolute part to texmf and \ for the relative parts below that, this is acceptable to windows.

Related Question