Hi,
I am looking at my hw and wondering how to define make headings like The "Problem x.x". I'd imagine there is a way to do sth like: \problem{} and then latex will automatically number it for me.
http://inst.eecs.berkeley.edu/~cs281a/f ... /home2.pdf
It seems the only way to learn latex is to learn by examples. Any better ideas?
Thank you.
Outdateboy
General ⇒ Define my own section style
NEW: TikZ book now 40% off at Amazon.com for a short time.

-
- Posts: 707
- Joined: Tue Mar 25, 2008 5:02 pm
Define my own section style
The solution is to define a new counter, say "problem", and increment it every time you issue a command (say, \problem). You have to first create the counter with \newcounter, then increment it with \refstepcounter. You can refer to it with a \label this way. If you just want to set the counter's value, use \addtocounter or \setcounter; to step it once, use \stepcounter, which does the same thing as \refstepcounter except that it doesn't make a \label key.
The \newcounter command takes one required and one optional argument. The required argument is the name of the counter by which you will refer to it later. The optional argument refers to another counter; incrementing this counter will reset the old counter. For example, \newcounter{prob}[set] resets prob to zero every time set gets increased. This is useful if, for example, your equation counter gets reset each chapter (such as Eq. (3.2), where it should go back to (4.1) at the beginning of chapter 4).
There are several standard counters: chapter, section, equation, subsection, part, subsubsection, figure, table, etc.
I've included some code that demonstrates the newcounter and stepcounter commands.
Personally, I think learning LaTeX solely by example is a terrible idea. If you can only copy someone else's ideas, you won't be able to come up with your own!
There are many sites where you can look up references to specific LaTeX commands and/or concepts. My personal favorite is http://www-h.eng.cam.ac.uk/help/tpl/tex ... ltx-2.html
There's also a great introduction that explains many of these topics at http://tug.ctan.org/info/lshort/english/lshort.pdf
The \newcounter command takes one required and one optional argument. The required argument is the name of the counter by which you will refer to it later. The optional argument refers to another counter; incrementing this counter will reset the old counter. For example, \newcounter{prob}[set] resets prob to zero every time set gets increased. This is useful if, for example, your equation counter gets reset each chapter (such as Eq. (3.2), where it should go back to (4.1) at the beginning of chapter 4).
There are several standard counters: chapter, section, equation, subsection, part, subsubsection, figure, table, etc.
I've included some code that demonstrates the newcounter and stepcounter commands.
Code: Select all
\documentclass{article}
\begin{document}
\newcounter{set}
\setcounter{set}{2}
\newcounter{problem}[set]
\newcommand{\problem}{\refstepcounter{problem}{\vspace{2\baselineskip}\noindent\large \bfseries Problem~\arabic{set}.\arabic{problem}}\\}
\problem
\textit{Sum-product algorithm:} Consider the sum-product\ldots.
\problem
\textit{Max-marginals:} Consider the max-marginals\ldots.
\stepcounter{problem}
\problem
Demonstraction of \verb"\stepcounter"
\addtocounter{problem}{-1}
\problem
Counter increments can be negative!
\end{document}
There are many sites where you can look up references to specific LaTeX commands and/or concepts. My personal favorite is http://www-h.eng.cam.ac.uk/help/tpl/tex ... ltx-2.html
There's also a great introduction that explains many of these topics at http://tug.ctan.org/info/lshort/english/lshort.pdf
Define my own section style
Another option to create theorem-like structures is to use the amsthm package; please refer to the package documentation for further information. Using the features and commands implemented by this package, the formatting and numbering of theorem-like structures can be easily handled. A little example:
Edit: Arghhh! When I wrote this reply I didn't notice that the original post was several months old. Had I noticed it before I wouldn't have written this post. Moderators, please feel free to delete this post.
Code: Select all
\documentclass{report}
\usepackage{amsthm}
%definition of the new style
\newtheoremstyle{problem}% name
{6pt}% Space above
{6pt}% Space below
{}% Body font
{}% Indent amount
{\bfseries}% Theorem head font
{}% Punctuation after theorem head
{\newline}% Space after theorem head
{}% Theorem head spec
%declaring the style to be used
\theoremstyle{problem}
%declaring the new theorem-like structure and declaring that
%it should be numbered within chapters
\newtheorem{prob}{Problem}[chapter]
\begin{document}
\chapter{Dummy chapter}
\begin{prob}
This is the first test problem of chapter one text text text
\end{prob}
\begin{prob}
This is the second test problem of chapter one text text text
\end{prob}
\chapter{Another dummy chapter}
\begin{prob}
This is the first test problem of chapter two text text text
\end{prob}
\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...