Document Classes\newcommand in a .cls file with content given in a .tex file

Information and discussion about specific document classes and how to create your own document classes.
Tom_24601
Posts: 10
Joined: Tue Dec 27, 2011 3:37 pm

\newcommand in a .cls file with content given in a .tex file

Post by Tom_24601 »

I am trying to create a command in a style file that I can call inside other commands but its content is to be specified in a normal .tex file that uses this custom document class. Here is my minimum example (created from code I have seen in example documents):

Code: Select all

\ProvidesClass{min}
\LoadClass{article}

\newcommand{\example}[1]{\def\@example{#1}}
\newcommand{\exampletwo}{\@example}
for the .cls file and:

Code: Select all

\documentclass{min}

\example{example}

\begin{document}
\exampletwo
\end{document}
for the .tex file.
The problem is that instead of the .tex file reading the \example{example} as an indication to use this where appropriate in the .cls file, it automatically prints "example" to the PDF file (even though it is not even contained within the document environment - I know this is what is happening from my more complicated codes and also the fact that it still compiles even if I comment out \begin{document}).
Can anybody explain why this code is not working and point me in the direction of a solution (or even a better piece of code with a similar outcome)? Many thanks.
Last edited by Tom_24601 on Wed Dec 28, 2011 1:03 am, edited 1 time in total.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

\newcommand in a .cls file with content given in a .tex file

Post by Stefan Kottwitz »

Hi Tom,

welcome to the board!

\example{example} does not print anything, but \exampletwo does. Try, the latter commented out:

Code: Select all

\documentclass{min}
\example{example}
\begin{document}
text
\example{example}
%\exampletwo
\end{document}
Just text is printed. \exampletwo would print \@example, which is defined to be example.

Stefan
LaTeX.org admin
Tom_24601
Posts: 10
Joined: Tue Dec 27, 2011 3:37 pm

\newcommand in a .cls file with content given in a .tex file

Post by Tom_24601 »

Hi Stefan,

Thank you for your helpful response. The code you suggested works perfectly for my minimum example but in the context of more complicated documents I am having the same problem as before. Here is an extended version of the .cls file in my minimum example to illustrate what I mean:

Code: Select all

\ProvidesClass{min}
\LoadClass{article}

\newcommand{\example}[1]{\def\@example{#1}}
\newcommand{\exampletwo}{\Huge \@example}
I have added the \Huge command but when I compile the .tex file it prints at normal size which is what leads me to believe that it is registering the \example{example} part but not the \exampletwo part. It will still compile the code even if I comment out the \begin{document} and \exampletwo lines on the .tex file. I have narrowed the problem down to the

Code: Select all

\newcommand{\example}[1]{\def\@example{#1}}
line but I am not sure why this is a problem as I have seen it used in example files.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

\newcommand in a .cls file with content given in a .tex file

Post by Stefan Kottwitz »

Hi Tom,

did you try the codes pieces above yourself?
Tom_24601 wrote:The code you suggested works perfectly for my minimum example but in the context of more complicated documents I am having the same problem as before.
I just used your code, which works as expected. If your problem occurs in more complicated code, the cause is there. I cannot help you if you post reduced code, which doesn't have the error. There's nothing to fix.
Tom_24601 wrote: Here is an extended version of the .cls file in my minimum example to illustrate what I mean
Tom_24601 wrote: I have added the \Huge command but when I compile the .tex file it prints at normal size
Which .tex file? The above .tex file prints \Huge then.

Just post a combination of cls and tex file which show the error when used together.

Stefan
LaTeX.org admin
Tom_24601
Posts: 10
Joined: Tue Dec 27, 2011 3:37 pm

\newcommand in a .cls file with content given in a .tex file

Post by Tom_24601 »

Hi Stefan,

Here are the exact codes I am now trying to use - the .cls file:

Code: Select all

\ProvidesClass{min}
\LoadClass{article}

\newcommand{\example}[1]{\def\@example{#1}}
\newcommand{\exampleone}{\Huge \@example}
and the .tex file:

Code: Select all

\documentclass{min}

\example{example}

\begin{document}
\exampleone
\end{document}
I am still having the problem that it is printing at normal size.

Is it possible that there is a package I need to use to make this work, or a particular compiling software (I currently use TexStudio)? I can't see why this would be the case, but I don't understand why these codes would work for you and not for me otherwise.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

\newcommand in a .cls file with content given in a .tex file

Post by Stefan Kottwitz »

Tom_24601 wrote:I am still having the problem that it is printing at normal size.
For me it prints \Huge text. I clearly can see it if I add

Code: Select all

\normalsize more text
Can you post your .log file for this example as attachment here, plus the PDF output? If necessary for posting as attachment, rename the file extension.
Tom_24601 wrote:Is it possible that there is a package I need to use to make this work, or a particular compiling software (I currently use TexStudio)?
To make this example work, you don't need a package. Also the editor doesn't matter.

Stefan
LaTeX.org admin
Tom_24601
Posts: 10
Joined: Tue Dec 27, 2011 3:37 pm

Re: \newcommand in a .cls file with content given in a .tex

Post by Tom_24601 »

Yes, here they are.

Thank you for your continued help.
Attachments
Min.pdf
Output PDF
(11.55 KiB) Downloaded 361 times
Min.log
Log file
(3.07 KiB) Downloaded 366 times
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

\newcommand in a .cls file with content given in a .tex file

Post by Stefan Kottwitz »

I just can guess, your changed .cls is not used, either because you did not save changes or another one is in the path and loaded.

My log file of your code shows that it loads the larger font cmr17:

Code: Select all

./min.cls
Document Class: min 
(/usr/local/texlive/2011/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2011/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option)
...
</usr/local/texlive/2011/texmf-dist/fonts/type1/public/amsfont
s/cm/cmr10.pfb></usr/local/texlive/2011/texmf-dist/fonts/type1/public/amsfonts/
cm/cmr17.pfb>
Output written on mintest.pdf (1 page, 16559 bytes).
Yours doesn't load cmr17:

Code: Select all

./min.cls
Document Class: min 
(/usr/local/texlive/2011/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2011/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2007/10/19 v1.4h Standard LaTeX file (size option)
...
</usr/local/texlive/2011/texmf-dist/fonts/type1/public/amsfonts/cm
/cmr10.pfb>
Output written on Min.pdf (1 page, 11828 bytes).
Perhaps check if the min.cls file on disk has the \Huge command, not just the one in the editor.

Stefan
LaTeX.org admin
Tom_24601
Posts: 10
Joined: Tue Dec 27, 2011 3:37 pm

Re: \newcommand in a .cls file with content given in a .tex

Post by Tom_24601 »

Thank you Stefan, this suggestion was absolutely right - my computer had saved the .cls file in two places and the wrong one was loading; after saving it correctly and deleting the other file the minimum example worked as expected.

I have now exhaustively searched my computer for a similar problem with my original file and cannot find a file that has saved in the wrong directory or anything similar. I have copied my codes into new .cls and .tex files and saved them in place of the other files, and now the code is compiling without any error messages but no PDF file is being produced.

You have convinced me that there is nothing wrong with the code, so are the issues I am currently having still related to LaTeX in which case do you have any more advice for me, or is there generally an issue with my computer which I need to investigate in more detail?

Thank you again for all of your help.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10334
Joined: Mon Mar 10, 2008 9:44 pm

Re: \newcommand in a .cls file with content given in a .tex

Post by Stefan Kottwitz »

Check the .log file to see which files are actually loaded. Further you can read errors and warnings there, which could explain why there's no PDF file produced. You could post such a .log file here as attachment.

Stefan
LaTeX.org admin
Post Reply