我想用 cleveref 构建一个研究问题模板

我想用 cleveref 构建一个研究问题模板

我想为研究问题和子问题构建一个模板,可以在不同的论文中重复使用。我希望它具有以下功能:

  • 自动编号,使用 cleveref 使它们可引用
  • 在不同部分中至少两次拼写出完整的 RQ(不增加其计数器或建立新标签)

我使用以下代码基本取得了成功,但对有些事情我感到不满意:

  • cleveref 不尊重我的定义,总是将“RQ”大写(在最底部的句子中)。我可以让 cleveref 处理同\Question一种subQuestion类型的计数器吗,或者让它尊重大写字母?
  • 第一次定义 RQ 时,我必须手动传递一个虚拟参数。有没有更好的方法?LaTeX 可以注意到命令的第一次调用吗?
  • 每个问题/子问题的定义都很长且很笨拙。您能想到更优雅的方法吗?

我当前的输出

\documentclass{article}
\usepackage{xparse}
\usepackage{cleveref}
\crefname{rqcounter}{RQ}{RQs}
\crefname{rqcounter}{RQ}{RQs}
\Crefname{subrqcounter}{RQ}{RQs}
\Crefname{subrqcounter}{RQ}{RQs}

\begin{document}

\newcounter{rqcounter}
\newcounter{subrqcounter}
\renewcommand\thesubrqcounter{\arabic{rqcounter}.\arabic{subrqcounter}}

\NewDocumentCommand{\Question}{omm}{%
    \IfNoValueTF{#1}{\def\labelorref{\ref{#3}}}{\def\labelorref{\refstepcounter{rqcounter}\setcounter{subrqcounter}{0}\arabic{rqcounter}\label{#3}}}
    \noindent\textit{\textbf{RQ\labelorref:} #2\\}
}

\NewDocumentCommand{\subQuestion}{omm}{%
    \IfNoValueTF{#1}{\def\labelorref{\ref{#3}}}{\def\labelorref{\refstepcounter{subrqcounter}\arabic{rqcounter}.\arabic{subrqcounter}\label{#3}}}
    \noindent\textit{\textbf{RQ\labelorref:} #2\\}
}

\NewDocumentCommand{\rqone}{o}{
\def\myref{rq:one}
\def\qtext{How to make \textbackslash cleveref respect my capitalization rules?}
\IfNoValueTF{#1}{\Question{\qtext}{\myref}}{\Question[#1]{\qtext}{\myref}}
}

\NewDocumentCommand{\rqoneone}{o}{
\def\myref{rq:one:one}
\def\qtext{Is there a way to not pass a dummy argument for first time invocation?}
\IfNoValueTF{#1}{\subQuestion{\qtext}{\myref}}{\subQuestion[#1]{\qtext}{\myref}}
}

\NewDocumentCommand{\rqonetwo}{o}{
\def\myref{rq:one:two}
\def\qtext{Is there a way to make the definitions more elegant?}
\IfNoValueTF{#1}{\subQuestion{\qtext}{\myref}}{\subQuestion[#1]{\qtext}{\myref}}
}


\section{Methods}
Here, I introduce and give more details to the following research questions (RQ):\\
\rqone[firsttime]
\rqoneone[firsttime]
\rqonetwo[firsttime]

\section{Results}
Here I come back to my research questions, and answer them:\\
\rqone
I can't for now.\\
\rqoneone
\rqonetwo

\section{Discussion}
As we have seen: \Cref{rq:one,rq:one:one,rq:one:two} are not solved by me, yet.

\end{document}

答案1

使用这个答案,我可以定义一个自定义的“标签”,可以查找,无论它是否被定义。

可以使用 来修复大写字母\usepackage[capitalize]{cleveref}。这会使 RQ 和子 RQ 处于不同的引用类型中,但对我来说这似乎没问题。

总的来说,以下是我的新代码生成的新输出

\documentclass{article}
\usepackage[capitalize]{cleveref}
\crefname{rqcounter}{RQ}{RQs}
\crefname{rqcounter}{RQ}{RQs}
\Crefname{subrqcounter}{RQ}{RQs}
\Crefname{subrqcounter}{RQ}{RQs}

\begin{document}

\newcounter{rqcounter}
\newcounter{subrqcounter}
\renewcommand\thesubrqcounter{\arabic{rqcounter}.\arabic{subrqcounter}}

\makeatletter%
\newcommand{\iflabelexists}[3]{\@ifundefined{myr@#1}{#2}{#3}}%
\makeatother%

\newcommand\Question[2]{%
\def\labelorref{\iflabelexists{#1}{\refstepcounter{rqcounter}\setcounter{subrqcounter}{0}\arabic{rqcounter}\label{#1}\expandafter\xdef\csname myr@#1\endcsname{}}{\ref{#1}}}
  \noindent\textit{\textbf{RQ\labelorref:} #2\\}%
}

\newcommand\subQuestion[2]{%
\def\labelorref{\iflabelexists{#1}{\refstepcounter{subrqcounter}\arabic{rqcounter}.\arabic{subrqcounter}\label{#1}\expandafter\xdef\csname myr@#1\endcsname{}}{\ref{#1}}}
  \noindent\textit{\textbf{RQ\labelorref:} #2\\}%
}

\newcommand\rqone{\Question{rq:one}{How to make cleveref respect my capitalization rules?}}

\newcommand\rqoneone{\subQuestion{rq:one:one}{Is there a way to not pass a dummy argument for first time invocation?}}

\newcommand\rqonetwo{\subQuestion{rq:one:two}{Is there a way to make the definitions more elegant?}}

\section{Methods}
Here, I introduce and give more details to the following research questions (RQ):\\
\rqone
\rqoneone
\rqonetwo

\section{Results}
Here I come back to my research questions, and answer them:\\
\rqone
I can't for now.\\
\rqoneone
\rqonetwo

\section{Discussion}
As we have seen: \Cref{rq:one,rq:one:one,rq:one:two} are not solved by me, yet.

\end{document}

相关内容