0

I have a shared preamble file that provide uniform configuration for my documents. But it has no document class of its own. That way, I can determine the class for each document. But that means it doesn't stand alone. I would like to be able to have a document class loaded if none is already loaded, and to evaluate test content when in stand-alone mode.

How might this be accomplished?

This has pseudo-code in the comments explaining what I want to happen:

%% kile looks for a file if there is a filename dot t-e-x on this line
%% SHARED PREAMBLE FILE header.tex
%%
%% To use this, set the environment:
%% ~/.bashrc export TEX_HOME=$HOME/Documents/TeX
%% export TEXINPUTS=.:$TEX_HOME/share/:$TEXINPUTS
%%
%% In the document being produced load a document class. for example
%% \documentclass[letterrpaper,10pt]{book}
%%
%% add this to the document being produced
%% \input{header.tex}
%%


%% If no document class is loaded set standalone to TRUE
%% if standalone then load a document class for testing
\documentclass{article}
%% endif standalone

%% Begin shared preamble used by every document class
\usepackage{amsmath}
\usepackage{stix2}% must follow amsmath or arrows get flinky.

%% a bunch of shared configuration

%% All my documents need these
\newcommand{\st}{\mathrel{\backepsilon}} %% such that
\newcommand{\bc}{\mathrel{\because}} %% because
\newcommand{\tf}{\mathrel{\therefore}} %% therefore

%% if standalone load the test content
\begin{document}
Test my commands:
\[
\bc a=b \st c \tf d
\]

\end{document}
%% eindif standalone

%% if not standalon continue processing the document with
%% \input{header.tex}

2 Answers 2

3
%\documentclass{book}
\makeatletter % not needed in a package
\ifx\documentclass\@twoclasseserror
\else
\documentclass{article}
\fi

\begin{document}
blub
\end{document}
2
  • I don't see how that addresses all of what I need. I want to set a flag variable if the else condition holds. Then use that flag to conditionally process other content. Commented Apr 25 at 22:27
  • Sorry I looked only at the title of the question. But you can easily set some flag. E.g. \newif\ifstandalone first, and \standalonetrue in the else part. Commented Apr 25 at 22:32
1

This is only an expansion of Ulrike Fischer's comment on her answer:

\begin{filecontents}[overwrite]{sthheader.tex}
%% If no document class is loaded set standalone to TRUE
%% if standalone then load a document class for testing
\newif\ifsthstandalone
\makeatletter
\ifx\documentclass\@twoclasseserror
  % stuff to do if another class is loaded
  \sthstandalonefalse
\else
  \sthstandalonetrue
  \documentclass{article}
\fi
\makeatother
%% endif standalone

%% Begin shared preamble used by every document class
\usepackage{amsmath}
\usepackage{stix2}% must follow amsmath or arrows get flinky.

%% a bunch of shared configuration

%% All my documents need these
\newcommand{\st}{\mathrel{\backepsilon}} %% such that
\newcommand{\bc}{\mathrel{\because}} %% because
\newcommand{\tf}{\mathrel{\therefore}} %% therefore

%% if standalone load the test content
\ifsthstandalone
\begin{document}
Test my commands:
\[
\bc a=b \st c \tf d
\]

\end{document}
\fi
%% eindif standalone
\end{filecontents}

% \documentclass{article}
%% if not standalon continue processing the document with
\input{sthheader.tex}
\begin{document}
abc
\end{document}

produces the content from the header file as-is or abc if the commented \documentclass{article} line is uncommented.

Off-topic

  1. People will tell you not to use such a file at all. This is the recommended practice. I believe I should recommend it. I don't practise it.
    • If you are unpersuaded then ...
  2. IMHO, your proposed distribution method is a terrible way to share your work with others. If you want to keep standard settings together in a file, do one or more of the following:
    • Create a proper class or package you can include with your documents. This is a format people are accustomed to working with.
    • Combine your documents with your standard settings prior to distribution. Obviously, you may not wish to do this manually. Either script it or use one of the LaTeX packages/distributed scripts to do it for you.
    • If you are unpersuaded then ...
  3. Just tell people to drop the file in their working directories; job done. If you later send them another file with a different version, no problem: each document has the appropriate version of your settings.
    • If you are unpersuaded then ... It would really, really be better not to do this. However, if you absolutely insist and are going to do it anyway, then ...
  4. It would be better to tell people to put your file in their personal TDS trees, wherever those happen to be, rather than telling them to create a new location with a non-standard name, which is confusingly similar to a standard one.
    • For example, kpsewhich -var-value TEXMFHOME will return the personal TDS tree. So, on a Unix-y system, for instance,
    $ mkdir -p $(kpsewhich -var-value TEXMFHOME)/tex/latex/sthheader && mv -n sthheader.tex $(kpsewhich -var-value TEXMFHOME)/tex/latex/sthheader/
    
    would install sthheader.tex in an appropriate place. There is then no reason to change TEXINPUTS.
    • Note that $HOME/Documents will only work on Unix-y systems with the os language set to some version of English.
      • On my machine, there is no such directory.
      • The whole point of the xdg desktop standards on Linux, for instance, is that they support standard directory names in users' preferred interface languages, whatever those happen to be.
      • Hopefully other platforms have something similar by now, though they certainly did not used to.
    • Note that ~/.bashrc export TEX_HOME=$HOME/Documents/TeX is wrong even if the person happens to be in a bash shell on a Unix-y system with the interface language set to English.
      • bash is not the default shell on Macs or all Linux distros and may not even be installed.
      • .bashrc is supposed to be sourced - not executed.
      • Even if ~/Documents happens to exist, ~/Documents/TeX may not etc. etc. Why are you making people create this whole hierarchy of files just to compile a single .tex file they receive?
      • You shouldn't require people to clobber their environments just to compile your document.
4
  • This is entirely self-inflicted. I just have a bunch of documents for which I want uniform settings. I modify the header. Save it with errors telling me that it isn't complete. Go back to my original document and use my new modifications. So I'm not "unit testing". Commented Apr 30 at 7:00
  • @StevenThomasHatton well, you are entitled to make your own life as difficult as you please, of course. but probably it is as well to have an answer warning other people not to use this to distribute their work. I'd still recommend putting this in your personal tds tree rather than abusing TEXINPUTS. that is literally what it is designed for. Commented Apr 30 at 7:12
  • The reason I put the entries in .bashrc is because I was unaware of $HOME/texmf and TEXMFHOME. When I grepped the environment I didn't see anything TEX. I read documentation saying that TEXINPUTS would work, and it did. Commented Apr 30 at 7:17
  • @StevenThomasHatton they don't need to be in your environment. best is if they aren't. kpsewhich -var-value TEXMFHOME will tell you where your personal tds tree is. (given your current settings.) yes, you can manipulate TEXINPUTS (and I do), but not as a general, default environment thing. Commented Apr 30 at 7:37

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.