如何根據計數器的值變更其顯示方式?

如何根據計數器的值變更其顯示方式?

我需要一個環境,​​其中環境計數器的呈現方式會根據計數器的值而變化。如果計數器小於 10,則必須在其後添加序數標記“°”,否則列印時不帶序數標記。

我嘗試使用amsthmifthen軟體包來執行此操作:

\newtheorem{artigo}{Art.}
\renewcommand{\theartigo}{%
\ifx\artigo<10%
\arabic{artigo}º%
\else%
\arabic{artigo}%
\fi%
}

但它不起作用,我得到“藝術”。 1”,而不是“藝術。 1°”,當我使用環境時artigo

我究竟做錯了什麼?

答案1

整數值的測試是透過以下命令完成的

\ifnum counter value [relation] number ... \else ... \fi

IE

\ifnum\value{artigo} < 10
  true branch
\else
  false branch
\fi

或可以使用

\makeatletter
\renewcommand{\theartigo}{%
  \ifnum\c@artigo<10
    \arabic{artigo}º%
  \else
    \arabic{artigo}
  \fi
}
\makeatother

擺脫\value{artigo}宏,但它必須定價\makeatletter...\makeatother,所以沒有節省打字;-)(\c@artigo是保存計數器值的內部計數器宏)

\documentclass{article}

\usepackage[utf8]{inputenc}



\newtheorem{artigo}{Art.}
\renewcommand{\theartigo}{%
  \ifnum\value{artigo}<10
    \arabic{artigo}º%
  \else
    \arabic{artigo}%
  \fi
}

\begin{document}

\begin{artigo}
Foo
\end{artigo}

\setcounter{artigo}{9}
\begin{artigo}
Foo again
\end{artigo}


\end{document}

在此輸入影像描述

相關內容