`\string` 有什么问题?

`\string` 有什么问题?

我正在使用该algorithm2e包来排版我的算法。我希望能够控制标题编号样式。更具体地说,我希望标题编号用花括号括起来并且是蓝色的,即标题应该说

算法{1} ...

'{1}' 部分为蓝色,而不是

算法1......

从我目前的研究来看,这似乎是

\renewcommand{\thealgocf}{\string{\@arabic{\numexpr\value{algocf}\relax}\string}}

或者

\renewcommand{\thealgocf}{\string{\@arabic\c@algocf\string}}

应该可以解决问题(\{\}没有奏效,好像转义不起作用:

! TeX capacity exceeded, sorry [input stack size=5000].
\curr@fontshape ->\f@encoding 
                              /\f@family /\f@series /\f@shape 
l.337 \end{AlgoFunctionLike}

\string{产生一个\string}一个 ̋ 标记(甚至不能将后者放入逐字引用中......),所以这也不起作用 - 它说

算法 –1̋

我真的不知道任何文本...这里发生了什么,以及(如何)我才能得到我想要的东西?

哦,还有

\renewcommand{\thealgocf}{\textcolor{blue}\string{\@arabic\c@algocf\string}}

给我

\renewcommand{\thealgocf}{\textcolor{blue}\string{\@arabic\c@algocf\string}}

所以颜色似乎也不起作用……

附言:我在文档中没有看到执行此操作的任何选项algorithm2e,有一些选项可以设置数字后的分隔符,还有一些选项可以设置标题的一般字体样式(忘记了命令名称),但没有任何选项可以改变标题数字的样式......

Pps:这是一个小的可行示例(如果您有正确的软件包......)

\documentclass[a4paper,11pt]{scrartcl}

\makeindex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% USED PACKAGES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[english]{babel}
\usepackage{tikz-cd}
\usetikzlibrary{babel} %conflicts...
\usepackage[utf8]{inputenc}
% BibTex
\usepackage{natbib}
\usepackage{makeidx}

% \usepackage{caption} %provides command \captionsetup

% More math typography
%\usepackage{urwchancal}
% Needed to get mathpzc
%% !!! Put this *above* the ams* packages, because urwchancal.sty
%% overwrites the mathcal command
\DeclareFontFamily{OT1}{pzc}{}
\DeclareFontShape{OT1}{pzc}{m}{it}{<-> s * [1.15] pzcmi7t}{}
\DeclareMathAlphabet{\mathpzc}{OT1}{pzc}{m}{it}

\usepackage{amsmath}
% Number equations according to section, see
\numberwithin{equation}{section}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{enumitem}
% Better two-header arrows (longer...)
\usepackage{extpfeil}
\usepackage{mathtools}
\usepackage{relsize} %mathlarger command
\usepackage[english]{todonotes}
\usepackage{hyperref}
\numberwithin{equation}{section}
% Double Stroke. amsfont doesn't render numbers properly
\usepackage{dsfont}
\newenvironment{keywords}%
    {\begin{trivlist}\item[]{\bfseries\sffamily Keywords:}\ }
    {\end{trivlist}}
% Used by qspace macro
\usepackage{xfrac}
\usepackage{graphicx}

% Used for Bibtex
\usepackage{url}
% Custom packages
\usepackage{./latex-math/general_math}
% No use yet
% \usepackage{./latex-math/general_algebra}
% For defining environments as wrappers around old ones, see
\usepackage{environ}
% Used for colors for algorithm2e
\usepackage{xcolor}
\hypersetup{colorlinks=false, linkbordercolor={cyan}}

\usepackage[noline, algoruled, nofillcomment, linesnumbered]{algorithm2e}
% Syntaxcolors etc./customization pertaining to 'algorithm2e'
% \definecolor{syntax_keywords}{RGB}{0, 152, 220}
\newcommand\mykwfont[1]{\texttt{\color{brown}#1}}
\SetKwSty{mykwfont}

\usepackage{aligned-overset} % allow alignment markers + overset: \overset{bla}&{=}

\definecolor{dark_grey}{RGB}{128, 128, 128}
\newcommand\mynlfont[1]{\texttt{\color{dark_grey}#1}}
\SetNlSty{mynlfont}{}{}

\definecolor{purple_dark}{RGB}{125, 0, 125}
% \newcommand\myfuncfont[1]{\texttt{\color{syntax_function}#1}}

\definecolor{syntax_comment}{RGB}{128, 128, 128}
\newcommand\mycommfont[1]{\texttt{\color{blue}#1}}
\SetCommentSty{mycommfont}

% \definecolor{syntax_blockmarkers}{RGB}{255, 255, 255}
% \newcommand\mybmfont[1]{\texttt{\color{syntax_blockmarkers}#1}}
% \SetBlockMarkersSty{mybmfont}

\SetArgSty{text}
\SetProgSty{text}
\newcommand\myalcapnamefont[1]{\textbf{#1}}
\SetAlCapNameSty{myalcapnamefont}
\newcommand\myfuncstyfont[1]{\textbf{#1}}
\SetFuncSty{myfuncstyfont}
\SetFuncArgSty{text}

\setlength{\algomargin}{0.3em}

\setcounter{tocdepth}{4}
\setcounter{secnumdepth}{4}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ALGORITHM2E STUFF
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\Parameters}{\textbf{Parameters}\\}
\newcommand{\FuncParam}[2]{{\ArgSty{#1}}:\hspace{1em}{\color{blue}#2}\\}

\newcommand{\Subroutines}{\textbf{Subroutines}\\}
\newcommand{\DeclareFunction}[1]{\SetKwFSimple{#1}\texttt{#1}()}
\newcommand{\Output}[1]{\textbf{Output}\\{\color{blue}#1}}
\newcommand{\KwReturn}[1]{\KwReturnHelper{#1\\\vspace{0.5em}}}
\newcommand{\SetKwFSimple}[1]{\SetKwFunction{#1}{#1}}
\newcommand{\KwMainFunctionCaption}[2]{\caption{#1}\SetKwFunction{KwMainFunc}{#1}}
\SetKw{continue}{continue}
\SetKw{KwInt}{int}
\newcommand{\CComment}[1]{{\DontPrintSemicolon\tcc{#1}}}
\newcommand{\Cland}{\&\& }

\NewEnviron{AlgoFunctionLike}{
    \IncMargin{1em}
    \SetInd{1em}{1em}
    \SetAlgoHangIndent{0.75em}
    \SetStartEndCondition{ \textbf{(}}{\textbf{)}}{\textbf{)}}
    \SetAlgoBlockMarkers{}{}%
    \SetKwProg{Fn}{}{{ \textcolor{black}\{}}{{\textcolor{black}\}}}
    \SetKwFor{For}{for}{{ \textcolor{black}\{}}{{\textcolor{black}\}}}%
    \SetKwIF{If}{ElseIf}{Else}{if}{{ \color{black}\{}} {{\color{black}\}} else if}{{ \textcolor{black}\}} {else} {\textcolor{ black}\{}}{{\textcolor{black}\}}}%
    \SetKwFor{While}{while}{{ \textcolor{black}\{}} {{\textcolor{black}\}}}%
    \SetKwRepeat{DoWhile}{do{ \textcolor{black}\{}}{{\textcolor{black}\}} while}%
    \SetKwComment{tcc}{\color{blue}/* }{ \color{blue}*/}
    \AlgoDisplayBlockMarkers
    \SetAlgoNoLine%
    \begin{algorithm}
        \BODY
    \end{algorithm}
}

% Hack to quickly typeset functions
\newcommand{\AlgoFunctionLikeFont}[1]{{\textcolor{blue}\{#1\}}}


% Parameters as in C-for loop, same order
\newcommand{\ForParams}[3]{{#1};\hspace{0.5em}{#2};\hspace{0.5em}{#3}}
% Only use in algorithm env.
\newcommand{\ForLoop}[4]{\For{\ForParams{#1}{#2}{#3}}{#4}}
% Parameters: As for ForParams
% Prints a for-loop beginning like for (bla1; bla2; bla3) {
% With curly braces and parens with \huge; currently looks like the internal
% typesetting for blocks
\newcommand{\BeginForLoop}[3]{%
    \KwSty{for}\hspace{0.3em}\textbf{(}%
        \ForParams{\ArgSty{#1}}{\ArgSty{#2}}{\ArgSty{#3}}\textbf{)} {\large\{}\\%
}
\newcommand{\BeginForLoopNoLineBreak}[3]{%
    \KwSty{for}\hspace{0.3em}\textbf{(}%
        \ForParams{\ArgSty{#1}}{\ArgSty{#2}}{\ArgSty{#3}}\textbf{)} {\large\{}%
}
% Open a code block, but only visually, i.e. no block w.r.t. algorithm2e will be opened
\newcommand{\BlockSty}[1]{{\large{}#1}}%
\newcommand{\OpenBlockVisual}{\BlockSty{\{}}%
% Close-counterpart to the above
\newcommand{\CloseBlockVisual}{\BlockSty{\}}}%
\newcommand{\QuickFunc}[2]{\FuncSty{{#1}(}\ArgSty{#2}\FuncSty{)}}%

\begin{document}
\selectlanguage{english}

\author{Alexander Rehbein}

\newcommand{\Gets}{\ensuremath{}\;\boldmath{\longleftarrow}\;}
\newcommand{\varmatrix}[5]{
    \ensuremath{\left({#1}_{i,j}\right)_{\substack{{#2}=1,\,\dots\,,\,{#3} \\ j={#4},\,\dots\,,\,{#5}}}}
}
\newcommand{\matring}[3]{\ensuremath{{#1}^{{#2}\times{#3}}}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ALGO EXAMPLE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand{\ring}{\ensuremath{\mathpzc{R}}}

\begin{AlgoFunctionLike}\label{alg:MatrixMult}
\KwMainFunctionCaption{MatrixMult}{$A,\,B$}

\Parameters
\FuncParam{$A$}{Element of $\matring{\ring}{m}{n}$, $A = \varmatrix{a}{i}{m}{j}{n}$, $m$, $n$ positive integers, $\ring$
                a ring} 
\FuncParam{$B$}{Element of $\matring{\ring}{n}{o}$, $B = \varmatrix{b}{j}{n}{k}{o}$, $o$ positive integer}
\Output{$AB$}
\vspace{1em}

\Fn{\KwMainFunc{$A,\,B$}} {
    $C$ = $\varmatrix{c}{i}{m}{k}{o} \Gets 0$\;
    \ForLoop{row $\Gets 1$}{row $\leq m$}{++row} {
        \ForLoop{col $\Gets 1$}{col $\leq o$}{++col} {
            \ForLoop{sum\_index $\Gets 1$,\,$c_{\text{row}, \text{col}} \Gets 0$}
                    {sum\_index $\leq n$}{++sum\_index} {
                $c_{\text{row}, \text{col}} \Gets c_{\text{row}, \text{col}} + %
                    a_{\text{row}, \text{sum\_index}} \cdot b_{\text{sum\_index}, \text{col}}$\;
            }
        }
    }
    \Return{$C$}\;
}
\end{AlgoFunctionLike}
\bibliographystyle{unsrtnat}
\bibliography{sources}

\end{document}

答案1

使用答案如何改变 algorithm2e 中算法编号和名称的顺序?来帮助指导我。

\documentclass{article}
\usepackage{algorithm2e,xcolor}

\makeatletter
\renewcommand{\fnum@algocf}{\AlCapSty{\algorithmcfname\nobreakspace
  \color{blue}\{\AlCapFnt\thealgocf\}}}
\makeatother

\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
      }{
      go back to the beginning of current section\;
      }
    }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

在此处输入图片描述

答案2

下面更新计数器表示以用蓝色打印算法编号和括号:

在此处输入图片描述

\documentclass{article}

\usepackage{algorithm2e,xcolor}

\renewcommand{\thealgocf}{\textcolor{blue}{\{\arabic{algocf}\}}}

\begin{document}

See Algorithm~\ref{alg:algorithm}.

\begin{algorithm}
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{%
      go to next section\;
      current section becomes this one\;
    }{%
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
  \label{alg:algorithm}
\end{algorithm}

\end{document}

由于表示形式已更改,因此自然也会移动到任何\reference 的表示形式。如果您使用自己的引用宏(例如\algoref),那么您可以将其合并为

\newcommand{\algoref}[1]{Algorithm~{\ttfamily\small\ref{#1}}}

相关内容