通过pdf文本中的字符更改tikz中绘制的符号

通过pdf文本中的字符更改tikz中绘制的符号

我正在画一组骰子,如以下代码:

\documentclass{article}
\usepackage   {ifthen}
\usepackage   {tikz}

\tikzset%
{%
  pics/die/.style={%
    code={%
      \begin{scope}[x=1ex,y=1ex]
      \draw[line width=0.15ex,rounded corners=0.3ex] (-0.75,-0.75) rectangle (0.75,0.75);
      \ifthenelse{#1 > 3}
      {%
        \fill (-0.4, 0.4) circle (0.15); % top left dot
        \fill ( 0.4,-0.4) circle (0.15); % bottom right dot
      }{}% do nothing
      \ifthenelse{#1 > 1}
      {%
        \fill ( 0.4, 0.4) circle (0.15); % top right dot
        \fill (-0.4,-0.4) circle (0.15); % bottom left dot
      }{}
      \ifthenelse{#1 = 6}
      {%
        \fill (-0.4,0) circle (0.15);    % center left dot
        \fill ( 0.4,0) circle (0.15);    % center right dot
      }{}
      \ifthenelse{#1 = 1 \OR #1 = 3 \OR #1 = 5}{\fill (0,0) circle (0.15);}{} % center dot
      \end{scope}
      }}
}

\begin{document}
\huge

\foreach\i in {1,...,6}{\tikz \pic[red] {die={\i}};\,}

Snake Eyes: \tikz\pic {die={1}}; \tikz\pic {die={1}};

Fever Five: \tikz\pic {die={4}}; \tikz\pic {die={1}};
\end{document}

在此处输入图片描述

我的问题是否可以为每个骰子分配一个字符,以便您可以在 pdf 中选择它作为文本。例如,我想在 pdf 中选择“Fever Five: [die 4] [die 1]”行,然后在另一个文档中复制粘贴为“Fever Five: 4 1”。

我知道有用于此目的的unicode字符,并且可能有许多包可以执行相同操作,但我想以这种方式进行(如果可能的话)。

答案1

那么使用技巧怎么样?

在骰子后面写上透明但可复制的文字。

\documentclass{article}
\usepackage{ifthen}
\usepackage{tikz}
\usepackage{transparent}

\tikzset%
{%
  pics/die/.style={%
    code={%
      \begin{scope}[x=1ex,y=1ex]
      \draw[line width=0.15ex,rounded corners=0.3ex] (-0.75,-0.75) rectangle (0.75,0.75);
      \ifthenelse{#1 > 3}
      {%
        \fill (-0.4, 0.4) circle (0.15); % top left dot
        \fill ( 0.4,-0.4) circle (0.15); % bottom right dot
      }{}% do nothing
      \ifthenelse{#1 > 1}
      {%
        \fill ( 0.4, 0.4) circle (0.15); % top right dot
        \fill (-0.4,-0.4) circle (0.15); % bottom left dot
      }{}
      \ifthenelse{#1 = 6}
      {%
        \fill (-0.4,0) circle (0.15);    % center left dot
        \fill ( 0.4,0) circle (0.15);    % center right dot
      }{}
      \ifthenelse{#1 = 1 \OR #1 = 3 \OR #1 = 5}{\fill (0,0) circle (0.15);}{} % center dot
      \end{scope}
      }}
}

\newlength{\lungnum}
\newcommand{\dado}[2][black]{\settowidth{\lungnum}{#2}\texttransparent{0}{#2}\hspace{-\lungnum}\tikz{\pic[#1] {die={#2}};}}

\begin{document}
\huge

\foreach\i in {1,...,6}{\dado[red]{\i}\,}

Snake Eyes: \dado{1} \dado{1}

Fever Five: \dado{4} \dado{1}

\end{document}

复制粘贴生成的 pdf:

在此处输入图片描述

你得到:

1 2 3 4 5 6
Snake Eyes: 1 1
Fever Five: 4 1

正如 Juan 所建议的,它也适用于使用带有透明文本的隐藏节点(opacity=0)。在这种情况下,您甚至不必加载transparent包:

\documentclass{article}
\usepackage{ifthen}
\usepackage{tikz}

\tikzset%
{%
  pics/die/.style={%
    code={%
      \begin{scope}[x=1ex,y=1ex]
      \draw[line width=0.15ex,rounded corners=0.3ex] (-0.75,-0.75) rectangle (0.75,0.75);
      \ifthenelse{#1 > 3}
      {%
        \fill (-0.4, 0.4) circle (0.15); % top left dot
        \fill ( 0.4,-0.4) circle (0.15); % bottom right dot
      }{}% do nothing
      \ifthenelse{#1 > 1}
      {%
        \fill ( 0.4, 0.4) circle (0.15); % top right dot
        \fill (-0.4,-0.4) circle (0.15); % bottom left dot
      }{}
      \ifthenelse{#1 = 6}
      {%
        \fill (-0.4,0) circle (0.15);    % center left dot
        \fill ( 0.4,0) circle (0.15);    % center right dot
      }{}
      \ifthenelse{#1 = 1 \OR #1 = 3 \OR #1 = 5}{\fill (0,0) circle (0.15);}{} % center dot
      \end{scope}
      }}
}

\newlength{\lungnum}
\newcommand{\dado}[2][black]{\tikz{\node[opacity=0,inner sep=0cm]{#2};\pic[#1] {die={#2}};}}

\begin{document}
\huge

\foreach\i in {1,...,6}{\dado[red]{\i}\,}

Snake Eyes: \dado{1} \dado{1}

Fever Five: \dado{4} \dado{1}

\end{document}

opacity=0显然,您可以直接在您的代码中插入节点pic,而不必更改其余部分的任何内容:

\documentclass{article}
\usepackage   {ifthen}
\usepackage   {tikz}

\tikzset%
{%
  pics/die/.style={%
    code={%
      \begin{scope}[x=1ex,y=1ex]
      \node[opacity=0,inner sep=0cm]{#1};
      \draw[line width=0.15ex,rounded corners=0.3ex] (-0.75,-0.75) rectangle (0.75,0.75);
      \ifthenelse{#1 > 3}
      {%
        \fill (-0.4, 0.4) circle (0.15); % top left dot
        \fill ( 0.4,-0.4) circle (0.15); % bottom right dot
      }{}% do nothing
      \ifthenelse{#1 > 1}
      {%
        \fill ( 0.4, 0.4) circle (0.15); % top right dot
        \fill (-0.4,-0.4) circle (0.15); % bottom left dot
      }{}
      \ifthenelse{#1 = 6}
      {%
        \fill (-0.4,0) circle (0.15);    % center left dot
        \fill ( 0.4,0) circle (0.15);    % center right dot
      }{}
      \ifthenelse{#1 = 1 \OR #1 = 3 \OR #1 = 5}{\fill (0,0) circle (0.15);}{} % center dot
      \end{scope}
      }}
}

\begin{document}
\huge

\foreach\i in {1,...,6}{\tikz \pic[red] {die={\i}};\,}

Snake Eyes: \tikz\pic {die={1}}; \tikz\pic {die={1}};

Fever Five: \tikz\pic {die={4}}; \tikz\pic {die={1}};
\end{document}

相关内容