GeneralHow to put code in the preamble instead of in a .sty?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
daantje
Posts: 11
Joined: Wed Aug 12, 2009 1:22 pm

How to put code in the preamble instead of in a .sty?

Post by daantje »

Hi,

I want to put some code from a .sty in the main .tex file, but I don't know how to do this. I have copy-pasted the code from the .sty file into the preamble of my .tex file, but I get errors, while everything was fine when I just used the .sty file.

Here's the relevant part of my .tex:

Code: Select all

%\usepackage{natbibspacing}

\newdimen\bibspacing
\setlength\bibspacing\z@
\renewenvironment{thebibliography}[1]{%
 \bibfont\bibsection\parindent \z@\list
   {\@biblabel{\arabic{NAT@ctr}}}{\@bibsetup{#1}%
    \setcounter{NAT@ctr}{0}}%
    \ifNAT@openbib
      \renewcommand\newblock{\par}
    \else
      \renewcommand\newblock{\hskip .11em \@plus.33em \@minus.07em}%
    \fi
    \sloppy\clubpenalty4000\widowpenalty4000
    \sfcode`\.=1000\relax
    \let\citeN\cite \let\shortcite\cite
    \let\citeasnoun\cite
            \itemsep\bibspacing %
            \parsep\z@skip %
 }{\def\@noitemerr{%
  \PackageWarning{natbib}
     {Empty `thebibliography' environment}}%
  \endlist\vskip-\lastskip}
The errors I get are many (there are three indentical ones for each bibliographic reference). However, the first bunch of errors looks like this:

Code: Select all

[LaTeX] myfile-0.6.2.tex => myfile-0.6.2.dvi (latex)
[LaTeX] finished with exit status 1
myfile-0.6.2.tex:11:Undefined control sequence. \setlength\bibspacing\z
myfile-0.6.2.tex:11:Missing number, treated as zero. \setlength\bibspacing\z
myfile-0.6.2.tex:11:Illegal unit of measure (pt inserted). \setlength\bibspacing\z
myfile-0.6.2.tex:11:Missing \begin{document}. \setlength\bibspacing\z@
myfile-0.6.2.tex:293:Undefined control sequence. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Missing number, treated as zero. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Illegal unit of measure (pt inserted). \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Undefined control sequence. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Extra \else. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Extra \fi. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Undefined control sequence. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Missing number, treated as zero. \begin{thebibliography}{28}
myfile-0.6.2.tex:293:Illegal unit of measure (pt inserted). \begin{thebibliography}{28}
myfile-0.6.2.tex:299:Something's wrong--perhaps a missing \item. ...Jan, Piet, and Klaas}]{Henk2007}
myfile-0.6.2.tex:299:Something's wrong--perhaps a missing \item. ...Jan, Piet, and Klaas}]{Henk2007}
myfile-0.6.2.tex:299:Something's wrong--perhaps a missing \item. ...Jan, Piet, and Klaas}]{Henk2007}
The resulting .dvi file has two problems: (1) it starts with nearly empty page, that has an"@" on it. (2) The references are messed up and they contain code.

I would use just .sty file instead of putting the code in my .tex, but the submission system of the journal I am going to submit my paper to (Ecology) only allows one LaTeX file to be uploaded.

Help would be much appreciated!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: How to put code in the preamble instead of in a .sty?

Post by josephwright »

You will need \makeatother before the code and \makeatother after it. These "hide" commands such as \z@ inside the main part of the document. Inside .sty files, @ is automatically a letter.
Joseph Wright
daantje
Posts: 11
Joined: Wed Aug 12, 2009 1:22 pm

Re: How to put code in the preamble instead of in a .sty?

Post by daantje »

Thank you for your quick reply, Joseph!

/makeatother doesn't work, but after a bit of Googling, I gave /makeatletter a try, and it solves my problem!

Thanks a million!
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

How to put code in the preamble instead of in a .sty?

Post by josephwright »

As I said, you need \makeatletter before the code, and should really use \makeatother after as it puts things "back to normal". Odd stuff can happen if you have the wrong category code for @.
Joseph Wright
daantje
Posts: 11
Joined: Wed Aug 12, 2009 1:22 pm

Re: How to put code in the preamble instead of in a .sty?

Post by daantje »

\makeatletter before and \makeatother after.

That makes more sense. Thanks.
Post Reply