將變數內容轉換為新命令內的逐字表示(然後用作可選參數)

將變數內容轉換為新命令內的逐字表示(然後用作可選參數)

我花了 8 個小時來解決這個問題,取得了一些進展,但現在面臨著乳膠中逐字記錄的巨大困難。任何幫助都會很棒!

我的目標:該todonotes套件提供了一個內聯選項,但是如果想使用,該選項就會中斷\listoftodos,因為更複雜的內聯事物(例如方程式環境)在某種程度上不適合標題參數。一個簡單的解決方案是將 fix 的可選標題參數設為\todo諸如 之類的東西\todo[inline,caption={inline...}]{...}。更難但更好的解決方案是剪切內嵌註釋的前 20 個字元並將其放入標題中。這是我嘗試過的並且有效幾乎

具體來說,只要剪切版本看起來不包含特殊字符,它就已經可以工作了。在下面的程式碼中,您會發現%%%%%%%標記了決定性的位置。\StrLeft{\myraw}{10}有效,但\StrLeft{\myraw}{20}已經壞了。

此外,我還進行了一些其他嘗試,如何使該內容像逐字逐句一樣。他們在外面工作\newcommand,但不在裡面工作。我也知道\NewDocumentCommand{\myverb}{+v}{\texttt{#1}}from xparse,但是在這裡我也找不到實現我想要的東西的方法。

期待您的幫助。

\documentclass{article}

\usepackage{xstring}
\usepackage{amsmath}
\usepackage{comment}
\usepackage{todonotes}
\usepackage{verbdef}


% TODO this still does not work for special sequences! kind of escape inbetween?
\newcommand{\MYTODO}[2][]{%
% makes a raw approximation and cut - this must be called within the same newcommand and cannot be outsourced for some reason I do not fully understand:
\scancs[0]\myraw{#2}
%equivalent to:
%\StrExpand[0]{#2}{\h}
%\def\raw{\detokenize\expandafter{\h}}
\StrLeft{\myraw}{10}[\myleft]  %%%%%%% changing this number to for example 20 breaks the code %%%%%%%
%
% this is the essential thing we want:
%\expandafter\verbdef\expandafter\variable\expandafter+\myleft+    %%%breaks immedidiately
%\todo[inline, caption={\string\myleft}, #1]{#2}   %%%breaks immediately
%\todo[inline, caption={\texttt{\myleft}}, #1]{#2}  %%%breaks also for surroundings
\todo[inline, caption={\myleft}, #1]{#2}
}


\begin{document}
\listoftodos
\bigskip

\MYTODO[author=me]{ABCDEF}

\MYTODO{%
but what if
\begin{align*}
    x^2
\end{align*}
}


\bigskip
%I tested here some ways to get verbatime output inside a variable, but both do not work in commands
\def\toverb{this works|, but only outside a newcommand.}
\expandafter\verb\expandafter+\toverb+
\expandafter\verbdef\expandafter\fromverb\expandafter+\toverb+
\fromverb

\end{document}

\StrLeft{\raw}{10} 程式碼的輸出

答案1

我在這裡不是嘗試逐字解決方案,而是tokcycle循環瀏覽您的待辦事項註釋輸入並對其進行處理以產生合適的標題。產生待辦事項清單標題的處理包括:

  1. cat 10、11 和 12 標記會回顯到標題中

  2. 支撐{}改造成cat-12[]

  3. 巨集名稱被\string編輯,但是\被替換為/.trunclim無論巨集名稱有多長,相對於 allowed ,巨集僅算 1 個標記。

  4. 除 0、1、2、10、11、12 之外的類別代碼將被忽略。

  5. 總數代幣待辦事項清單的標題中的內容不會超過\thetrunclim - 1,考慮到巨集名稱在此計數中僅算作單一標記。

  6. 在隨後的 MWE 中,trunclim設定為20

氣象局:

\documentclass{article}
\usepackage{amsmath}
\usepackage{comment}
\usepackage{todonotes}
\usepackage{tokcycle}
\newcounter{trunclength}
\def\trunclim{20}
\makeatletter
\newcommand\myleft[1]{%
  \setcounter{trunclength}{0}%
  \tokcycle
  {\stepcounter{trunclength}%
   \tctestifnum{\thetrunclength<\trunclim}{%
     \tctestifcatnx A##1{\addcytoks{##1}}{%
     \tctestifcatnx 0##1{\addcytoks{##1}}{%
     }}%
   }{}}%
  {\processtoks{[##1]}}%
  {\stepcounter{trunclength}%
    \tctestifnum{\thetrunclength<\trunclim}{%
    \addcytoks{/}%
    \addcytoks[2]{\expandafter\@gobble\string##1}}{}}%
  {\stepcounter{trunclength}%
    \tctestifnum{\thetrunclength<\trunclim}{\addcytoks{##1}}{}}%
  {#1}%
}
\makeatother
% TODO this still does not work for special sequences! kind of escape inbetween?
\newcommand{\MYTODO}[2][]{%
  \myleft{#2}\todo[inline, caption={\the\cytoks}, #1]{#2}
}
\begin{document}
\listoftodos
\bigskip
\MYTODO[author=me]{ABCDEF}
\MYTODO{%
but what if
\begin{align*}
    x^2
\end{align*}
}
\end{document}

在此輸入影像描述

如果trunclim設定為 40,結果如下:

在此輸入影像描述

相關內容