0

This question already has an answer here:

What I'm trying to do is to create a \newcommand (not only theorems) and everytime I call that command it adds the chapter number and number of times that I call that command.

For instance, if I'm writting chapter #3 and I created a newcommand called \theorem, this is the output I'm expecting for the first time I call the command:


3.1 Theorem:

Text

I know how to create the new command, I was googling this but didn't find anything relevant. This is the command I'm using:

\newcommand{\mycommand}[2]{%
  \textbf{Theorem: #1}\\
   #2
}

marked as duplicate by Andrew, Jesse, Mensch, Jagath, Zarko Aug 11 '16 at 4:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • For theorems specifically some of this functionality is provided by the amsthm package. – Arun Debray Aug 11 '16 at 2:01
  • Welcome to TeX.SX!! Rather than posting code fragments it is better to give a full minimal working example. Currently we have to guess what packages etc you are using and this makes it much harder to help you. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much more likely that they will! – Andrew Aug 11 '16 at 2:04
1

Here's a solution that works for general commands, not just theorems.

The idea is to define a counter for your command, use \numberwithin to reset it every chapter, and then define your command to increment and show the counter:

\documentclass{book}
\usepackage{amsmath} % for \numberwithin
\newcounter{eggs}
\numberwithin{eggs}{chapter}
\newcommand{\wine}[1]{
    \stepcounter{eggs}
    {\bfseries Theorem \arabic{chapter}.\arabic{eggs}}: #1
}
\begin{document}
\chapter{Carrot}

\wine{Corn}

\wine{Ziti}
\chapter{Tomato}

\wine{Stew}

\end{document}

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.