4

This is what I have.

\documentclass[12pt,a4paper]{article}
\newcounter{theorem}[section]
\newcommand\theorem{\stepcounter{theorem}\framebox{\textbf{Theorem \thesection.\thetheorem}}}

\begin{document}
\section{New Section}

\theorem \label{t1}

\theorem \label{t2}

\ref{t1}

\ref{t2}

\end{document}

It shows :

1  New Section
Theorem 1.1
Theorem 1.2
1
1

I would like it to show this instead :

1  New Section
Theorem 1.1
Theorem 1.2
1.1
1.2

How can I do that? Thanks.

(I'm aware of the \newtheorem command but I don't want my text automatically italicized and I want my text to start on a new paragraph, which is why I went with making my own \theorem command, but now I'm having difficulty referencing the two custom counters that I have, together. I also accidentally made a duplicate account to which I can't login to to delete the duplicate post I made as I'm not sure how to logout from my actual account.)

  • Welcome! Can you please make your snippet compilable? – cfr Jul 17 '18 at 02:45
  • 1
    You need \refstepcounter rather than \stepcounter. – cfr Jul 17 '18 at 02:46
  • It isn't obvious what is producing the 1 and 1 in your 'output', so it isn't obvious what should be producing the 1.1 and 1.2 you want instead. – cfr Jul 17 '18 at 02:47
  • Apologies. I've edited it now. I was using \ref to get the output. What does \refstepcounter do? How is it different to \stepcounter? – Chung Ren Khoo Jul 17 '18 at 02:52

1 Answers1

5

To tell LaTeX that this is an element you want to cross-reference, you need \refstepcounter rather than \stepcounter. Otherwise, your labels just get references to the section counter which, as you've seen, is 1 throughout the example.

To ensure the cross-references include the section counter, you can customise the output of \thetheorem to include the counter.

For example,

\documentclass[12pt,a4paper]{article}
\newcounter{theorem}[section]
\renewcommand\thetheorem{\thesection.\arabic{theorem}}
\newcommand\theorem{\refstepcounter{theorem}\framebox{\textbf{Theorem \thetheorem}}}

\begin{document}
\section{New Section}

\theorem \label{t1}

\theorem \label{t2}

\ref{t1}

\ref{t2}

\end{document}

produces

sections and theorems

cfr
  • 198,882