Former question: Autoloading and adding code to a package in the user init.m
I am using MaTeX, and I found the updateMaTeX[] function] at the GitHub repo.
I tried to add this to my init.m in the MaTeX` context, and wrote
BeginPackage["MaTeX`"];
updateMaTeX[] :=
Module[{json, download, target},
Check[
json = Import["https://api.github.com/repos/szhorvat/MaTeX/releases/latest", "JSON"];
download = Lookup[First@Lookup[json, "assets"], "browser_download_url"];
target = FileNameJoin[{CreateDirectory[], "MaTeX.paclet"}];
If[$Notebooks,
PrintTemporary@Labeled[ProgressIndicator[Appearance -> "Necklace"], "Downloading...", Right],
Print["Downloading..."]
]; URLSave[download, target],
Return[$Failed]
]; If[FileExistsQ[target], PacletManager`PacletInstall[target], $Failed]
]; updateMaTeX::usage = "suggested at the MaTeX GitHub repo."
EndPackage[]; DeclarePackage["MaTeX`", {"MaTeX", "ConfigureMaTeX", "ClearMaTeXCache","updateMaTeX"}];
However this leads to two issues:
- (Solved) The supposedly local variables
json``download``targetare touched in theMaTeX`context, and picked up by the auto-completion. It's a simple fix to addRemove[json, download, target]to the code, but I wonder why no$ModuleNumberwas appended to the symbols. - Autoloading does not work as expected. Type
MaTeXin a new notebook and it is still undefined; evaluation causes this new symbol to be created inGlobal`. TypingMaTeX`MaTeX, it is undefined, but become defined as soon as it is evaluated;Global`MaTeXis simultaneously shadowed. At this point, the $\text{M}\!^\text{A}\!\text{T}\!_{\normalsize{\text{E}}}\!\text{X}$ document is accessible, but the symbolMaTeX(MaTeX`MaTeX) does not have any downvalues, and thus is not working.
Similar issues occur with the Rubi` package. Therefore I wonder:
- Is using
BeginPackage["Package`"]outside the package.ma good practice? - Why
DeclarePackagedoes not work as expected?