3

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 2:45
  • 1
    You need \refstepcounter rather than \stepcounter. – cfr Jul 17 '18 at 2: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 2: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 2:52
3

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

  • Thank you! One quick question though, how do you get the last bit to show up on your answer? Is that a screenshot of the compiled PDF? – Chung Ren Khoo Jul 17 '18 at 3:04
  • 1
    @ChungRenKhoo Yes. It is a screenshot (PNG) which I uploaded when posting my answer. – cfr Jul 17 '18 at 3:10
  • 1
    @ChungRenKhoo If you like an answer and it was helpful, please consider upvoting (by clicking on the arrows next to the score) and marking it as accepted answer (by clicking on the checkmark ✓). – Andrew Jul 17 '18 at 4:22
  • Yep. I have done that. (I think) It doesn't register my upvote publicly as I'm still below 15 rep (that's what it says anyways). – Chung Ren Khoo Jul 17 '18 at 4:32
  • You need upvotes/reputation to be able to do something in this forum. I've upvoted your question. ;-) – Ulrich Diez Jul 17 '18 at 8:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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