
我對 LaTeX 不夠精通,無法解決以下問題。我有一個宏,它接受兩個參數,創建一個漂亮的框,將第一個參數以粗體顯示,將第二個參數以強調文字顯示:
\newcommand{\Step}[2]{%
\noindent{\\[0pt] \rule{0pt}{0.5ex}%
\hspace*{1em}\fbox{\parbox[t]{0.92\columnwidth}{{\bfseries Step #1.\ }\emph{#2}}}}
\vspace*{1.1ex} }
我在文字中調用此巨集 10-15 次(我有這麼多步驟)。為了能夠引用該巨集展開的頁面,我現在想放置一個標籤。如果我將最後一行更改為:
\vspace*{1.1ex} \label{step:mode:#1} }
錯誤開始出現。我在一條完全不相關的線上得到了這個:
! Missing \endcsname inserted.
<to be read again>
\protect
l.160 ...aults Occur\relax }{figure.caption.52}{}}
該行可能來自一些輔助文件。
如何正確擴展第一個參數並將其用作標籤的參數?第一個參數沒有空格,也永遠不會有空格。
答案1
您可以\textsuperscript
在設定標籤時重新定義。這將\@firstofone
是一個很好的重新定義。該命令只是讀取參數並按原樣使用它。
\csname phantomsection\endcsname
與 package 建立工作連結也可能是個好主意hyperref
:
\documentclass{article}
\usepackage{lipsum}% for demonstration only
\usepackage{hyperref}% to show, that this works
\makeatletter
\newcommand{\Step}[2]{%
\par\noindent{\rule{0pt}{0.5ex}%
\csname phantomsection\endcsname
{\let\textsuperscript\@firstofone\label{#1}}%
\hspace*{1em}\fbox{\parbox[t]{\dimexpr \linewidth-2em-2\fboxsep-2\fboxrule\relax}{%
{\bfseries Step #1.\ }\emph{#2}}}}\par
\vspace*{1.1ex}
}
\makeatother
\begin{document}
\Step{R\textsuperscript{+}4.a}{blah, blah}
See R\textsuperscript{+}4.b on page \pageref{R+4.b}.
\lipsum
\Step{R\textsuperscript{+}4.b}{blah, blah}
See R\textsuperscript{+}4.a on page \pageref{R+4.a}.
\lipsum
\end{document}
如果不使用的話,就只是 ,所以它幾乎什麼都沒有\csname phantomsection\endcsname
。\relax
hyperref
答案2
我會以不同的方式做:
\usepackage{calc}
\newcommand{\Step}[2]{%
\par\addvspace{1.1ex}
\noindent\hspace*{1em}\fbox{\parbox[t]{\columnwidth-2em-2\fboxsep-2\fboxrule}
{\textbf{Step #1.}\ \emph{#2}}}\label{step:mode:#1}\\*[1.1ex]}
這樣,它們\fbox
將在線居中,並且前後具有相等的空間(如果您不喜歡它們現在的樣子,請更正間距)。
請注意,標籤中允許使用空格,但重音字元或指令中不允許使用空格。所以\Step{R\textsuperscript{+}4.a}{blah, blah}
一定會出問題。你可以透過說來解決它
\newcommand{\Step}[3]{%
\par\addvspace{1.1ex}
\noindent\hspace*{1em}\fbox{\parbox[t]{\columnwidth-2em-2\fboxsep-2\fboxrule}
{\textbf{Step #2.}\ \emph{#3}}}\label{step:mode:#1}\\*[1.1ex]}
並將這個有問題的條目稱為
\Step[R+4.a]{R\textsuperscript{+}4.a}{blah, blah}
然後指的是
\pageref{R+4.a}