\newcommand and \renewcommand difficulty in class file

I am making an "Assignment" class file. One element of my class is not working correctly. I want to be able to input the assignment # in the latex file, using \assigntitle{4} and have a centered Assignment #4 printed at the top of the page. I use a \newcommand and \renewcommand to implement this in the class file. Alas this portion is not working. I only get the predefined output from the \newcommand definition and not my value input from the latex file. I think it is because of a scoping issue, but I can't figure out how to get around it. I tried to use \global\def\ but this didn't work. I think \renewcommand can't use this type of \def. If I am wrong, please inform me.

I have removed extraneous code (and yes I have tried it with only this code) from the full file, of which I show below.

\ProvidesClass{Assignment} \NeedsTeXFormat{LaTeX2e}  \DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}} \ProcessOptions  \LoadClass[11pt,letterpaper]{article}  \newcommand{\@assignment}{Assignment \#} \newcommand{\assigntitle}[1]{\renewcommand{\@assignment}{Assignment \#{#1}}}   \AtBeginDocument{%     \centering \huge \upshape \@assignment \\     \normalsize \normalfont \@date     \bigskip }  \endinput 

example.tex

\documentclass[english]{Assignment}  \begin{document}  \assigntitle{4}  \end{document} 

Replay

For this implementation I'd suggest using \assigntitle not just to update some internal macro, but also to set the actual title:

\newcommand{\assigntitle}[1]{%
  \begin{center}
    \huge \upshape Assignment \# #1 \\
    \normalsize \normalfont \@date
  \end{center}
}

If you want to set a date other than \today, the you'd use

\date{January 1, 2001}
\assigntitle{4}

The basic problem is that \@assignment is used right at the start of the document body within \AtBeginDocument, so using \assigntitle{4} etc. after \begin{document} is ineffective, since the redefined \@assignment is never used then.

\centering should not occur outside of a group, so use \begingroup...\endgroup.

I suggest to use a pagestyle header (e.g. fancyhdr) rather which is updated, also using counters.

Assignment.cls

\ProvidesClass{Assignment}
\NeedsTeXFormat{LaTeX2e}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions

\LoadClass[11pt,letterpaper]{article}

\newcommand{\@assignment}{Assignment \#}
\newcommand{\assigntitle}[1]{\edef\@assignment{Assignment \#{#1}}}

\AtBeginDocument{%
  \begingroup
  \centering
  \huge \upshape \@assignment
  \medskip

  \normalsize \normalfont \@date

  \bigskip

  \endgroup
}

\endinput

driver.tex

\documentclass{Assignment}

\assigntitle{4}

\begin{document}
\end{document}

It's a “chicken and egg” problem: with this code you must state \assigntitle{4} before \begin{document}.

An alternative and perhaps better strategy is to it with \maketitle:

\ProvidesClass{Assignment}
\NeedsTeXFormat{LaTeX2e}

\DeclareOption*{\PassOptionsToClass{\CurrentOption}{article}}
\ProcessOptions

\LoadClass[11pt,letterpaper]{article}

\newcommand{\@assignment}{Assignment \#}
\newcommand{\assigntitle}[1]{\renewcommand{\@assignment}{Assignment \#{#1}}}

\renewcommand\maketitle{%
    \begingroup % <----- don't forget this one
    \centering \huge \upshape \@assignment \\
    \normalsize \normalfont \@date
    \bigskip
    \endgroup % <----- matching \begingroup
}

\endinput

Now your sample document can be in any of the two forms below

\documentclass{Assignment}

\assigntitle{4}

\begin{document}

\maketitle

\end{document}

or

\documentclass{Assignment}

\begin{document}

\assigntitle{4}

\maketitle

\end{document}

You can add \date{July 28, 2016} anywhere before \maketitle.

The \begingroup and \endgroup tokens I added are necessary if you don't want \centering going on for the whole document.

If you still need \maketitle, use a different name for the command producing the header.

Note that typesetting material using \AtBeginDocument is not recommended, because several packages that a user might add after declaring the class do their business using that hook and so they would act after the header has been typeset.

Category: macros Time: 2016-07-28 Views: 0

Related post

  • Merging \newcommand and \renewcommand 2014-05-20

    Is there a merged command \maybenewcommand that is either \newcommand or \renewcommand, depending on the situation: if the command is not yet defined, use \newcommand and use \renewcommand otherwise. I currently merge multiple papers into a large doc

  • Do java .class files have a lexer/parser? 2015-06-06

    I've been learning ANTLR, by writing my own (extremely simple!) programming language It made me curious about how the lexer/parser/AST is implemented for java. Obviously there is a grammar for .java files and this gets parsed down to ByteCode, which

  • Setting pdfinfo fields from a class file 2015-06-27

    In a class file, I have set (to automatically set PDF information fields): \hypersetup{pdfinfo={Title={\@doctitle}, Author={\@docauthor}, Subject={\@docsubject}, Producer={LaTeX}, Version={\@docversion}, Date={\@docdate}, Institution={\@universitynam

  • Creating a class file vs using packages 2015-08-12

    In my limited LaTeX experience, I've always found that I can accomplish what I want by just using the relevant set of packages, even for more complex formatting. If I were to try and create a configuration for someone else I would be hard-pressed to

  • How to count the number of sentences and words in a text file? 2016-01-29

    I'm trying to write a program that reads a text file and counts the words, lines, sentences, vowels, alphanumeric characters, and punctuation. I want to output to both the console and an output.txt file. So far I got it to count the words (sort of),

  • Conflict between TikZ and university thesis class file 2013-01-10

    When compiling a document using my university's thesis class file and the TikZ package I receive an error message (missing number, treated as zero). Would someone know how I can solve this problem? \documentclass[phd,bottom,nosig]{usbthesis} \usepack

  • Modular LaTeX document preambles and class files 2015-01-26

    I have been debating whether or not to ask this question as I don't intend to create an overly broad unanswerable, or solely opinion based question, but I have decided that there are probably some techniques that will adequately answer this question.

  • Is it best practice to always capitalize class file names in PHP, and other languages? 2012-05-27

    I ask because I've seen a couple of OO PHP tutorials which do not adhere to this, but to me it just makes sense. It makes it easy to instantly see if a file is a class or not, if non-class files all use lowercase names. --------------Solutions-------

  • Is it really bad to use \makeatletter and \makeatother in a package or class file? 2012-07-08

    In my answer to this question What do \makeatletter and \makeatother do? I apparently wrote: The commands should not be used within .sty and .cls files themselves as they may conflict with the catcode changes that occurs when package and class files

  • I would like to ask for suggestions, criticisms and/or possible edits on my class file to be used on an Android Application? 2013-01-23

    I have a class file that would be used to connect and execute queries into the database. The only thing I am confused about is this: do you really need to drop tables and re-create them everytime you run the application? Or does this depend on your c

  • IEEE Class File 'ieee.cls' Error When Using PGF and Friends (root cause is the color package) 2015-01-18

    I have to use the IEEE class file which can be found here: http://mocha-java.uccs.edu/ieee/ There you can download the class file itself (ieee.cls) and a demo tex file called ieeecls.tex. When I load the pgfplots package then I get the following erro

  • Set global option (and pass it to all packages) in a custom class file 2015-04-23

    I had a tex-file that basically started as follows \documentclass[ a4paper, hyperref, amsmath, headinclude=true, ngerman, final ]{scrbook} \usepackage{babel} \usepackage{ntheorem} \usepackage{graphix} I converted this into my own custom class file \N

  • Visual studio build and tfs build of class project files 2016-02-11

    net. so having this basic doubt. In C programming all .c files are compiled to get .o file. as output. the object file. Similarly for C# Class files what is the output generated, When i compile as csproj which has only class files, I dont see any dll

  • Take all classes from .owl file and compare with another classes using Java 2016-02-13

    I already generated an ont.owl file using Jena. Then first I need to take all the classes to the array list which are contain the ontology. secondly I will give another classes(terms) using my code and check whether these classes contain generated on

  • Modifying a thesis class file 2013-07-10

    A friend of mine gave me a class file he had used for his dissertation, which he submitted this spring. This is a class file he wrote; it was not provided by the university. Between spring & summer, the university changed the requirements for a disse

  • Thesis class file, signing page not showing up 2013-10-19

    http://grad.ucalgary.ca/current/thesis/latex-template For some reason the signing page (under Substitute List of Examiners) doesn't show up when you compile. The class file looks like this. I'm not sure how I could modify it to make the sign page sho

  • Changing definitions in the class file 2015-08-30

    I am relatively new to Latex and don't know the deeper commands yet. For my Msc thesis I downloaded a template published by the Faculty, however they have changed some regulations and have yet to update their latex template. Basically, the new regula

  • How to import additional code into class file 2012-03-27

    I was inspired by the article "Fancy chapter headings with TikZ" (Stefan Kottwitz many thx) and I have created the whole fancy sectioning. But what I need to do now is to import this whole code for sections and chapter into my REPORT CLASS becau

  • How do I add a for loop to a class file function? 2012-05-28

    I'm using this XeLaTeX class file to write my resume. One of the features is for section headings it colors the first 3 letters: \section{experience} This is hardcoded to the first 3 letters, and I'd like to make it a variable length: \section{4}{ope

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 2.470 (s). 13 q(s)