如何从 a、b、c 开始,最后得到 1a2a3、1b2b3、1c2c3,

如何从 a、b、c 开始,最后得到 1a2a3、1b2b3、1c2c3,

假设您有如下代码:

ABC, some text , LaLaLa
\tooltip*{A}{\includegraphics[scale=2]{images/A}}\tooltip*{B}{\includegraphics[scale=2]{images/B}}\tooltip*{C}{\includegraphics[scale=2]{images/C}}

现在请注意第一个 {争论}\tooltip再次出现在 {争论} 的\includegraphics

现在,在这种情况下,我手动输入AC\includegraphics,以匹配AC\tooltip。但是如果我们想找到一种方法让 Tex 自动完成这一任务呢?

或者更好的是,如果您想找到一种方法来创建一个最终用户只需输入以下内容的环境:

ABC

自动重现第一个代码?

我想这可以使用正则表达式,那么如何以 LaTeX 方式来实现呢?


==定义==

此处的工具提示是什么意思,可以在这个答案

答案1

不确定你想做什么,但这是一个相当通用的方法,基于代码https://tex.stackexchange.com/a/164186/4427

这不支持\tooltips**,但扩展它很容易。

第一个可选参数用于\includegraphics传递给每张图片的选项;尾随的可选参数用于设置项目之间的分隔(默认为无)。

\documentclass[a6paper,12pt]{scrbook}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% tooltips with LaTeX
%
% optimized for Adobe Reader (visible on mouse-over)
%     usage: \tooltip[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%   non-draggable version:
%     usage: \tooltip*[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%
% for Evince (visible on click, not draggable)
%   usage: \tooltip**[<link colour>]{<link text>}[<tip box colour>]{<tip text>}
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{pdfbase}[2017/03/16]
\usepackage{xparse,ocgbase}
\usepackage{xcolor,calc}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}

\ExplSyntaxOn
\let\tpPdfLink\pbs_pdflink:nn
\let\tpPdfAnnot\pbs_pdfannot:nnnn\let\tpPdfLastAnn\pbs_pdflastann:
\let\tpAppendToFields\pbs_appendtofields:n
\def\tpPdfXform{\pbs_pdfxform:nnnnn{1}{1}{}{}}
\let\tpPdfLastXform\pbs_pdflastxform:
\ExplSyntaxOff

\makeatletter
\NewDocumentCommand{\tooltip}{ssO{blue}mO{yellow!20}m}{{%
  \leavevmode%
  \IfBooleanT{#1}{%
    \ocgbase@new@ocg{tipOCG.\thetcnt}{%
      /Print<</PrintState/OFF>>/Export<</ExportState/OFF>>%
    }{false}%
    \xdef\tpTipOcg{\ocgbase@last@ocg}%
  }%
  \tpPdfLink{%
    \IfBooleanTF{#2}{%
      /Subtype/Link/Border [0 0 0]/A <</S/SetOCGState/State [/Toggle \tpTipOcg]>>
    }{%
      /Subtype/Screen%
      \IfBooleanTF{#1}{%
        /AA<<%
          /E<</S/SetOCGState/State [/ON \tpTipOcg]>>%
          /X<</S/SetOCGState/State [/OFF \tpTipOcg]>>%
        >>%
      }{
        /AA<<%
          /E<</S/JavaScript/JS(%
            var fd=this.getField('tip.\thetcnt');%
            \IfBooleanF{#1}{%
              if(typeof(click\thetcnt)=='undefined'){%
                var click\thetcnt=false;%
                var fdor\thetcnt=fd.rect;var dragging\thetcnt=false;%
              }%
            }%
            if(fd.display==display.hidden){%
              fd.delay=true;fd.display=display.visible;fd.delay=false;%
            }%
           this.dirty=false;%
          )>>%
          /X<</S/JavaScript/JS(%
            if(!click\thetcnt&&!dragging\thetcnt){fd.display=display.hidden;}%
            if(!dragging\thetcnt){click\thetcnt=false;}%
            this.dirty=false;%
          )>>%
          /U<</S/JavaScript/JS(click\thetcnt=true;this.dirty=false;)>>%
          /PC<</S/JavaScript/JS (%
            var fd=this.getField('tip.\thetcnt');%
            try{fd.rect=fdor\thetcnt;}catch(e){}%
            fd.display=display.hidden;this.dirty=false;%
          )>>%
          /PO<</S/JavaScript/JS(this.dirty=false;)>>%
        >>%
      }
    }%
  }{{\color{#3}#4}}%
  \sbox\tiptext{\fcolorbox{black}{#5}{#6}}%
  \edef\twd{\the\wd\tiptext}%
  \edef\tht{\the\ht\tiptext}%
  \edef\tdp{\the\dp\tiptext}%
  \measureremainder{\whatsleft}\tipshift=0pt%
  \ifdim\whatsleft<\twd\setlength\tipshift{\whatsleft-\twd}\fi%
  \tpPdfXform{\tiptext}%
  \raisebox{\heightof{#4}+\tdp}[0pt][0pt]{\makebox[0pt][l]{\hspace{\tipshift}%
    \tpPdfAnnot{\twd}{\tht}{\tdp}{%
      /Subtype/Widget/FT/Btn/T (tip.\thetcnt)%
      /AP<</N \tpPdfLastXform>>%
      /MK<</TP 1/I \tpPdfLastXform/IF<</S/A/FB true/A [0.0 0.0]>>>>%
      \IfBooleanTF{#1}{%
        /Ff 65537/OC \tpTipOcg%
      }{%
        /Ff 65536/F 3%
        /AA <<%
          /U <<%
            /S/JavaScript/JS(%
              var fd=event.target;%
              var mX=this.mouseX;var mY=this.mouseY;%
              var drag=function(){%
                var nX=this.mouseX;var nY=this.mouseY;%
                var dX=nX-mX;var dY=nY-mY;%
                var fdr=fd.rect;%
                fdr[0]+=dX;fdr[1]+=dY;fdr[2]+=dX;fdr[3]+=dY;%
                fd.rect=fdr;mX=nX;mY=nY;%
              };%
              if(!dragging\thetcnt){%
                dragging\thetcnt=true;Int=app.setInterval("drag()",1);%
              }%
              else{app.clearInterval(Int);dragging\thetcnt=false;}%
              this.dirty=false;%
            )%
          >>%
        >>%
      }%
    }%
    \tpAppendToFields{\tpPdfLastAnn}%
  }}%
  \stepcounter{tcnt}%
}}
\makeatother
\newsavebox\tiptext\newcounter{tcnt}
\newlength{\whatsleft}\newlength{\tipshift}
\newcommand{\measureremainder}[1]{%
  \begin{tikzpicture}[overlay,remember picture]
    \path let \p0 = (0,0), \p1 = (current page.east) in
      [/utils/exec={\pgfmathsetlength#1{\x1-\x0}\global#1=#1}];
  \end{tikzpicture}%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\ExplSyntaxOn
\NewDocumentCommand{\tooltips}{sO{}m+O{}}
 {
  \clist_map_inline:nn { #3 }
   {
    \IfBooleanTF{#1}{\tooltip*}{\tooltip}{##1}{\includegraphics[#2]{##1}}#4
   }
 }
\ExplSyntaxOff

\begin{document}

\tooltips[width=2cm]{example-image-a,example-image-b,example-image-c}

\bigskip

\tooltips[width=2cm]{example-image-a,example-image-b,example-image-c}[\par]

\end{document}

答案2

像这样?您当然不需要正则表达式来执行此操作。这是一个使用 的解决方案etoolbox\docsvlist我给出了 的简单定义\tooltip,因为您没有定义它,并且我认为它与问题没有直接关系。

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{etoolbox}
\newcommand{\tooltip}[1]{#1}
\newcommand{\ttips}[1]{%
  \renewcommand\do[1]{\tooltip{##1}\includegraphics[width=1cm]{images/##1}}
  \docsvlist{#1}}
\begin{document}
\ttips{A,B,C}
\end{document}

代码输出

答案3

LaTeX 已经定义了合适的循环,因此你只需要

在此处输入图片描述

\documentclass{article}

\def\tooltip#1{[ttip-#1]}
\usepackage{graphicx}

\makeatletter
\def\zz#1{%
 \@tfor\tmp:=#1\do{%
 \tooltip{\tmp}%
 \includegraphics[width=1cm]{example-image-\tmp}%
 }}

\begin{document}

\zz{abc}

\end{document}

\tooltip根据需要调整的定义。

答案4

使用包。可以使用重置listofitems列表分隔符(默认) 。根据需要重置。,\setsepchar{<delimiter>}\tooltip

\documentclass{article}
\usepackage{listofitems,graphicx}
\def\tooltip#1{[ttip-#1]}
\newcommand\retool[1]{%
  \readlist\mylist{#1}%
  \foreachitem\i\in\mylist{%
    \tooltip{\i}%
    \includegraphics[width=1cm]{example-image-\i}%
  }%
}
\begin{document}
\retool{a,b,c}
\end{document}

在此处输入图片描述

要使用没有分隔符的列表,请进行以下修改:

\documentclass{article}
\usepackage{listofitems,graphicx}
\def\tooltip#1{[ttip-#1]}
\newcommand\retool[1]{%
  \def\tmp{}%
  \ignoreemptyitems%
  \pretool#1\relax%
  \readlist*\mylist{\tmp}%
  \foreachitem\i\in\mylist{%
    \tooltip{\i}%
    \includegraphics[width=1cm]{example-image-\i}%
  }%
}
\makeatletter
\def\pretool#1#2\relax{%
  \g@addto@macro\tmp{#1,}%
  \if\relax#2\relax\else\pretool#2\relax\fi
}
\makeatother
\begin{document}
\retool{abc}
\end{document}

相关内容