参照できる新しいカウンター

参照できる新しいカウンター

\makeExampleという新しいカウンターを作成するメソッドを作成しましたexample。カウンターを参照できるようにメソッドを変更する方法を教えてください。exampleたとえば、次のように記述できますsome text.... \vref{label} ....

実際のサンプルはここにあります:https://www.sharelatex.com/project/541ffc3e05c84a5c4060ae58

私の方法は次のとおりです:

\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
   \LARGE\textsc{Eksempel~\thechapter.\theexample #1}}{\medskip}

\newcommand\makeExample{%
 {%
    \begin{center}%
        \begin{example}%
        \end{example}%
    \end{center}%
 }%
}

答え1

コードに対する最も簡単な変更は、\makeExampleなどの任意のコードを配置できる場所にオプションの引数を追加することです\label。 この例を下部に掲載していますが、これはおそらく必要なものではないと思います。 提供されているサンプルはmdframed環境をセットアップするもので、次のようにパッケージによって提供される定理のようなメカニズムを使用するのが最も簡単ですmdframed

サンプル出力

\documentclass{memoir}

\usepackage[utf8]{inputenc}
\usepackage{mathtools,varioref}
\usepackage[framemethod=TikZ]{mdframed}

\mdfdefinestyle{cexamplestyle}{%
 frametitlefont=\normalfont\Large\scshape,
 frametitlealignment=\center
}
\mdtheorem[style=cexamplestyle]{centerexample}{Example}[chapter]


\begin{document}

\vref{todo} is a sample. The fist is a sample of how I would like it to be

\begin{centerexample}
  \label{todo}
    \noindent
    \textbf{Data:} 1011
    \newline
    \textbf{Function:} $a\cdot x^3+b\cdot x^2+c\cdot x+d\cdot 1$

    \noindent
    Værdierne fra $a$, $b$, $c$ og $d$ findes som følger:
    \newline
    $a=1$, $b=0$, $c=1$ og $d=1$
\end{centerexample}

\end{document}

最初の変更によって得られる結果は次のとおりです。

\documentclass{book}

\usepackage{mdframed}
\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
   \LARGE\textsc{Example~\thechapter.\theexample #1}}{\medskip}

\newcommand{\makeExample}[1][\relax]{%
 {%
    \begin{center}%
      \begin{example}%
        #1%
      \end{example}%
    \end{center}%
 }%
}

\begin{document}

\chapter{A chapter}
\label{cha:chapter}

\begin{mdframed}
  \makeExample[\label{cex:sample}]
  A centered example.
\end{mdframed}

That was Example~\ref{cex:sample}.

\end{document}

2番目のサンプル

最初のアプローチの方がはるかに簡単だと思います。

答え2

質問するときは、完全な最小限の動作例(MWE) は、自分が何をしようとしているのかを示すため、また他の人が自分を助けるために役立ちます。誰かが何か作業の材料を与えてくれたら、助けるのはずっと簡単になります。MWE はステートメントで始まり\documentclass、必要なパッケージをすべて含め、 を持つ必要があります\begin{document}...\end{document}。MWE はコンパイルされ、求めているものを説明/実証するために必要な最小限のコードしか含まない必要があります。これにより、全員の時間を大幅に節約できます。

この場合、あなたは単に例を表示するための環境を定義しようとしているように私には思えます。もしこれがあなたがしようとしていることなら、あなたは頑張りすぎです\newtheorem{example}[section]{Eksempel}。単に を使用すればいいのです。相互参照を備えた完全な MWE は、

\documentclass{article}
\usepackage{amsmath}
\newtheorem{example}{Eksempel}% implicitly defines a counter
\numberwithin{example}{section}% numbers it within sections
\begin{document}
\section{Important examples}
\begin{example}\label{MyExample}% \label creates the reference
  1+1=2.
\end{example}

My Eksempel~\ref{MyExample} is great.% \ref creates the cross-reference

\end{document}

これにより、次のものが生成されます。

ここに画像の説明を入力してください

のドキュメントをご覧くださいアムスムパッケージを使用して、定理のような環境をカスタマイズする方法を確認してください。

編集スタイルをカスタマイズするには、Eksempelドキュメントを読む必要があります。アムスムパッケージ。次の行を追加すると:

\usepackage{amsmath,amsthm}
\newtheoremstyle{eksempel}
  {3pt}% ⟨Space above⟩
  {3pt}% ⟨Space below⟩
  {}% ⟨Body font⟩
  {0pt}% ⟨Indent amount⟩
  {\scshape}% ⟨Theorem head font⟩
  {}% ⟨Punctuation after theorem head⟩
  {0pt}% ⟨Space after theorem head⟩2
  {\centerline{\thmname{#1} \thmnumber{#2}}}% ⟨Theorem head spec⟩
\theoremstyle{eksempel}
\newtheorem{example}{Eksempel}% implicitly defines a counter

すると、定義しようとしていたスタイルが得られます。

ここに画像の説明を入力してください

\hfill「定理のヘッド仕様」の s が Eksempel の見出しの中央にあることに注意してください。

\newenvironment{example}これ以上のことをしようとすると、コードがやりすぎです。とコマンドの両方を定義する必要はありません\makeExample。環境自体がコマンドだからです。この場合、おそらく次のようなコードが望ましいでしょう。

\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
     \center\LARGE\textsc{Eksempel~\thechapter.\theexample #1}}
     {\endcenter\medskip}

\par(ただし、と\medskipsは間違っているように見えるので削除することをお勧めします。)

使用しているように、\refstepcounterこのコマンドを上記の MWE と同様に使用して相互参照を作成できます\label\refただし、\theexample環境の定義で を使用し、以下を追加する方がよいでしょう。

\renewcommand\theexample{\thechapter.\arabic{example}}

最後に、サンプル カウンターをセクションのサブカウンターとして定義しているので、次のことが本当に必要なのではないかと思います。

\renewcommand\theexample{\thesection.\arabic{example}}

答え3

これはラベル生成が自動化されたバージョンですが、カウンターをリセットすると失敗する可能性があります。

\documentclass{book}

\newcounter{example}[section]

\renewcommand{\theexample}{\thesection.\arabic{example}}
\newenvironment{example}[1][]{\refstepcounter{example}\label{mysophisticatedexample::\number\value{example}}
  \LARGE\textsc{Eksempel~\theexample #1}\par\medskip

}{\medskip}

\newcommand\makeExample{%
 {%
    \begin{center}%
        \begin{example}%
          This is some dummy example with \theexample as counter and it references \ref{mysophisticatedexample::1}
        \end{example}%
    \end{center}%
 }%
}

\begin{document}

\chapter{First}

\section{First}

\makeExample 

\makeExample

\makeExample





\end{document}

ここに画像の説明を入力してください

関連情報