Limit scope of formatting to within command

I wanted to generate a cover page for a tech report and so put the following in my preamble

\makeatletter
\newcommand{\coverpage}{\onecolumn \centering \large
\thispagestyle{empty}
{\noindent\includegraphics[width=3.5in]{Logo_black}}\vspace*{0.4in}\\
{\noindent\huge Electrical Engineering and Computer Science}\vspace*{0.8in}\\
{\noindent\Huge \bf \@title}\vspace*{0.3in}\\
{\noindent \@date}\vspace*{0.3in}\\
{\noindent \@author}\vspace*{0.4in}\\
\textit{TR-ID-12-06}\vspace*{0.4in}\\
\noindent\textbf{Abstract}\vspace*{0.1in}\\
\hspace*{0.1in}\begin{minipage}{0.8\linewidth}
\input{abstract}
\end{minipage}
\vfill
\setcounter{page}{0}}
\makeatother


Then, after \begin{document}, I have a \coverpage, followed by \maketitle and so on. However, now the whole document is formatted as one-column and centered even though my class file configured it to be two-column (and fully justified). Why does this happen and how should I avoid this?

-
Welcome to TeX.sx! – Peter Jansson Mar 14 '13 at 20:53
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. You could try putting a \begingroup, at the start of coverpage, and an \endgroup at the end. – Peter Grill Mar 14 '13 at 21:16
@PeterGrill thanks for the tip. Will keep in mind for future questions. – r.v Mar 14 '13 at 22:45

By surrounding the coverpage macro with a \begingroup and \endgroup you limit the effect of the macros to be within that scope.

Notes:

• The lipsum package was used just to provide dummy text. It is not needed in your document.
• The [demo] option was used for the graphicx package so as to place a black box where the figure would go for demo purposes, in your real usage (when you actually have the figures available), you need to remove this option.

Code:

\documentclass{article}

\usepackage{lipsum}
\usepackage[demo]{graphicx}

\makeatletter
\newcommand{\coverpage}{\begingroup\onecolumn \centering \large
\thispagestyle{empty}
{\noindent\includegraphics[width=3.5in]{Logo_black}}\vspace*{0.4in}\\
{\noindent\huge Electrical Engineering and Computer Science}\vspace*{0.8in}\\
{\noindent\Huge \bf \@title}\vspace*{0.3in}\\
{\noindent \@date}\vspace*{0.3in}\\
{\noindent \@author}\vspace*{0.4in}\\
\textit{TR-ID-12-06}\vspace*{0.4in}\\
\noindent\textbf{Abstract}\vspace*{0.1in}\\
\hspace*{0.1in}\begin{minipage}{0.8\linewidth}
%\input{abstract}
\end{minipage}
\vfill
\setcounter{page}{0}\endgroup}
\makeatother

\begin{document}
\title{Title}
\coverpage
\lipsum[1-7]
\end{document}

-
 \newcommand{\coverpage}{\onecolumn {\centering \large
%%


Start a group to limit the scope of \centering

\setcounter{page}{0}%
\par}%


End the group to stop \centering (always make sure that size changing and paragraph formatting commands include a \par at the end.

\twocolumn


start two column

}


end the command definition

Some unrelated comments, \noindent normally has no effect after \\ and has no real effect in centering as indentation is 0 anyway. It's best to avoid using \vspace in horizontal mode, its effects are defined but hard to explain. \vspace*{0.3in}\\ is better written as \\[.3in]

-
thanks for your tips. I learnt a lot from this one post. – r.v Mar 14 '13 at 22:46