更新

更新

我想创建一些像 Wordle 游戏中那样带框的字母: wordle 游戏截图:彩色方框中的字母 但到目前为止我还没有找到可以做到这一点的工具。我想将它们嵌入到标题和文档中。你会怎么做?

答案1

tikz这是与图书馆一起使用的可能性matrix

我定义了三种颜色:greatgood​​和bad,您可以根据自己的喜好进行调整。

wordle 是一个 TikZ 矩阵,bad默认情况下节点用 填充,除非您将其包含|[fill=<color>]|在单元格中。要在标题中使用,您必须使用ampersand replacement

节点样式通过tikzsetstyle进行全局设置wordlematrix。您可以在那里调整高度、宽度、间距和字体。

在此处输入图片描述

为了节省打字时间,我为fill=great和制作了样式fill=good

g/.style={fill=great}, h/.style={fill=good}

并将它们添加到tikzset。代码如下:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{wordlematrix/.style={matrix of nodes, nodes={anchor=center, fill=bad, minimum height=1cm, minimum width=1cm}, column sep=2pt, row sep=2pt, color=white, font=\sffamily\Large\bfseries},
    g/.style={fill=great}, h/.style={fill=good},  n/.style={fill=none, color=black, draw=dark-border, thick,minimum size=1cm-\pgflinewidth}, e/.style={fill=none, color=black, draw=light-border, thick,minimum size=1cm-\pgflinewidth}}

\definecolor{great}{rgb}{0.416, 0.667, 0.392}
\definecolor{good}{rgb}{0.788, 0.706, 0.345}
\definecolor{bad}{rgb}{0.471, 0.486, 0.494}
\definecolor{light-border}{rgb}{0.827, 0.839, 0.855}
\definecolor{dark-border}{rgb}{0.529, 0.541, 0.549}
\definecolor{none}{rgb}{1,1,1}

\title{Here is a 
    \begin{tikzpicture}[baseline=-1.5mm]
    \matrix(titlewordle)[wordlematrix, ampersand replacement=\&]{ 
    W \& |[g]|O \& |[g]|R \& D \& |[h]|L \& E\\
    };
    \end{tikzpicture}
    in a title}
\author{Sandy G}

\begin{document}
\maketitle

Here is a wordle in the document:
\[
\begin{tikzpicture}
\matrix(example)[wordlematrix]{ 
G & |[g]|R & |[g]|A & I & L\\
T & |[g]|R & |[g]|A & |[h]|C & K\\
|[g]|C & |[g]|R & |[g]|A & M & P\\
|[g]|C & |[g]|R & |[g]|A & B & S\\
|[g]|C & |[g]|R & |[g]|A & |[g]|Z & Y\\
|[g]|C & |[g]|R & |[g]|A & |[g]|Z & |[g]|E\\
};
\end{tikzpicture}
\]
Here is a wordle to complete:
\[
\begin{tikzpicture}
\matrix(example)[wordlematrix]{ 
W & R & O & N & G\\
T & |[g]|R & |[g]|A & |[h]|C & K\\
|[n]|T&|[n]|R&|[n]|Y&|[n]|M&|[n]|E\\
|[e]|& |[e]| & |[e]|& |[e]| & |[e]|\\
|[e]|& |[e]| & |[e]|& |[e]| & |[e]|\\
};
\end{tikzpicture}
\]
I did it!

\end{document}

答案2

欢迎来到 tex.stackexchange!

我刚刚用 Ti 完成了一个项目z 的chains库。因此,就像拿着锤子把世界视为钉子的人一样,我建议使用该chains库来实现这一点:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}

\usetikzlibrary{positioning,chains}

%% |=====8><-----| %%

\newcounter{wrownum}

%% |=====8><-----| %%

\NewDocumentEnvironment{wordframe}{}{% six rows, six chains
    \setcounter{wrownum}{0}%
    \begin{tikzpicture}[
        start chain=1 going right,
        start chain=2 going right,
        start chain=3 going right,
        start chain=4 going right,
        start chain=5 going right,
        start chain=6 going right,
        node distance=1mm]  
}{%
    \end{tikzpicture}%
}

\tikzset{wbox/.style={%
        text=white,font=\sffamily\bfseries,minimum size=2em,
    }
}

%% color shorthand -- change at will
\colorlet{gr}{gray}
\colorlet{gn}{green!80!blue}
\colorlet{yl}{yellow!80!orange}

\NewDocumentCommand{\wrow}{m}{%
    \stepcounter{wrownum}
    \foreach \L/\C in {#1}{% TikZ foreach: Letter/color
        \ifnum\thewrownum=1
            \node[wbox,fill=\C,on chain=\thewrownum]{\L};
        \else
            \node[wbox,fill=\C,
            below=1mm of \the\numexpr\thewrownum-1\relax-1,
            on chain=\thewrownum] {\L};
        \fi
    }%
}

\parindent0pt

\begin{document}

\noindent This is a frame \begin{wordframe}\wrow{W/gr,O/gr,R/gn,D/gr,L/yl,E/gn}\end{wordframe} in a sentence.

\bigskip

\begin{wordframe}
    \wrow{G/gr,R/gn,A/gn,I/gr,L/gr}
    \wrow{T/gr,R/gn,A/gn,C/yl,K/gr}
    \wrow{C/gn,R/gn,A/gn,M/gr,P/gr}
    \wrow{C/gn,R/gn,A/gn,B/gr,S/gr}
    \wrow{C/gn,R/gn,A/gn,Z/gn,Y/gr}
    \wrow{C/gn,R/gn,A/gn,Z/gn,E/gn}
\end{wordframe}

\end{document}

在此处输入图片描述

更新

我让数据的输入变得更容易一些(我讨厌打字)。此外,还有一些附加选项(通过“keyval.sty”),这些选项在示例中进行了演示。

\documentclass{article}

\usepackage{xcolor,xparse,tikz,keyval}

\usetikzlibrary{positioning,chains,backgrounds}

%% |=====8><-----| %%

\newcounter{wrownum}
\newlength{\wboxsep}
\newlength{\wboxsize}
\newlength{\wboxrcwd}
\newif\ifmkwframe
\newif\ifwdrawbox

%% |=====8><-----| %% keyval defaults -- change to suit %% |=====8><-----| %%

\setlength{\wboxsep}{1mm}%% <<<=>>> horizontal/vertical space between boxes
\setlength{\wboxsize}{2em}% default size of box; sets TikZ `minimum size'
\setlength{\wboxrcwd}{0pt}% default rounded cornet size; set to 0pt for no rounded corners
\def\wboxfontsize{\normalsize}% uses standard LaTeX sizes, e.g. \tiny, \small, \normalsize, \large...
\colorlet{wframefill}{white}


\makeatletter
\define@key{wrdlbox}{wboxsep}{\setlength{\wboxsep}{#1}}
\define@key{wrdlbox}{wboxrcwd}{\setlength{\wboxrcwd}{#1}}
\define@key{wrdlbox}{wboxsize}{\setlength{\wboxsize}{#1}}
\define@key{wrdlbox}{wboxfontsize}{\def\wboxfontsize{#1}}
\define@key{wrdlbox}{mkwframe}[true]{\csname mkwframe#1\endcsname}
\define@key{wrdlbox}{wdrawbox}[true]{\csname wdrawbox#1\endcsname}
\define@key{wrdlbox}{wframefill}{\colorlet{wframefill}{#1}}
\makeatother

%% |=====8><-----| %%
%% keyval ootions: wboxsep, wboxrcwd, wboxsize, wboxfontsize, mkwframe, wframefill, wdrawbox
\NewDocumentEnvironment{wordframe}{O{}}{% six rows, six chains; more rows? just add more chains
    \setkeys{wrdlbox}{#1}%
    \setcounter{wrownum}{0}%
    \begin{tikzpicture}[
        start chain=1 going right,
        start chain=2 going right,
        start chain=3 going right,
        start chain=4 going right,
        start chain=5 going right,
        start chain=6 going right,
        node distance=\wboxsep,outer sep=0pt]   
}{%
        \ifmkwframe
            \begin{scope}[on background layer]
                \draw[fill=wframefill,thick]
                ([xshift=-\wboxsep,yshift=-\wboxsep]current bounding box.south west)
                rectangle
                ([xshift=\wboxsep,yshift=\wboxsep]current bounding box.north east);
            \end{scope}
        \fi
    \end{tikzpicture}%
}

\tikzset{wbox/.style={%
        text=white,
        font=\sffamily\bfseries\wboxfontsize,
        minimum size=\wboxsize,
        fill=\C,
        rounded corners=\wboxrcwd,
        \ifwdrawbox draw\fi
    }%
}

%% color shorthand -- alter, change or enlarge at will
\colorlet{gr}{gray}
\colorlet{gn}{green!80!blue}
\colorlet{yl}{yellow!80!orange}

\NewDocumentCommand{\wrow}{u{>}}{% comma-separated values: Letter/color; 'u' requires xparse.sty
    \stepcounter{wrownum}
    \foreach \L/\C in {#1}{% TikZ foreach: Letter/color
        \ifnum\thewrownum=1
            \node[wbox,on chain=\thewrownum]{\L};
        \else
            \node[wbox,below=\wboxsep of \the\numexpr\thewrownum-1\relax-1,on chain=\thewrownum] {\L};
        \fi
    }%
}

\catcode`<=\active
\let<=\wrow

\parindent0pt

\begin{document}

\begin{wordframe}
    <G/gr,R/gn,A/gn,I/gr,L/gr>
    <T/gr,R/gn,A/gn,C/yl,K/gr>
    <C/gn,R/gn,A/gn,M/gr,P/gr>
    <C/gn,R/gn,A/gn,B/gr,S/gr>
    <C/gn,R/gn,A/gn,Z/gn,Y/gr>
    <C/gn,R/gn,A/gn,Z/gn,E/gn>
\end{wordframe}

\bigskip

\noindent This is a frame \begin{wordframe}<W/gr,O/gr,R/gn,D/gr,L/yl,E/gn>\end{wordframe} in a sentence.

\bigskip

\begin{wordframe}[wframefill=cyan!10,
    wboxsize=3em,wboxfontsize=\tiny,
    wboxsep=2mm,wboxrcwd=3pt,mkwframe]
    <G/gr,R/gn,A/gn,I/gr,L/gr>
    <T/gr,R/gn,A/gn,C/yl,K/gr>
    <C/gn,R/gn,A/gn,M/gr,P/gr>
    <C/gn,R/gn,A/gn,B/gr,S/gr>
    <C/gn,R/gn,A/gn,Z/gn,Y/gr>
    <C/gn,R/gn,A/gn,Z/gn,E/gn>
\end{wordframe}

\bigskip

\begin{wordframe}[wboxfontsize=\large,wboxsep=0pt,mkwframe,wdrawbox]
    <G/gr,R/gn,A/gn,I/gr,L/gr>
    <T/gr,R/gn,A/gn,C/yl,K/gr>
    <C/gn,R/gn,A/gn,M/gr,P/gr>
    <C/gn,R/gn,A/gn,B/gr,S/gr>
    <C/gn,R/gn,A/gn,Z/gn,Y/gr>
    <C/gn,R/gn,A/gn,Z/gn,E/gn>
\end{wordframe}

\end{document}

在此处输入图片描述

更新 2

冒着冗长乏味的风险,但为了输入最简单,我提供了以下内容。输出与前面完全相同。如果标签错误,则添加用红色填充字母框。

\documentclass{article}

\usepackage{xcolor,xparse,tikz,keyval}

\usetikzlibrary{positioning,chains,backgrounds}

%% |=====8><-----| %%

\newcounter{wrownum}
\newlength{\wboxsep}
\newlength{\wboxsize}
\newlength{\wboxrcwd}
\newif\ifmkwframe
\newif\ifwdrawbox

%% |=====8><-----| %% keyval defaults -- change to suit %% |=====8><-----| %%

\setlength{\wboxsep}{1mm}%% <<<=>>> horizontal/vertical space between boxes
\setlength{\wboxsize}{2em}% default size of box; sets TikZ `minimum size'
\setlength{\wboxrcwd}{0pt}% default rounded cornet size; set to 0pt for no rounded corners
\def\wboxfontsize{\normalsize}% uses standard LaTeX sizes, e.g. \tiny, \small, \normalsize, \large...
\colorlet{wframefill}{white}

\makeatletter
\define@key{wrdlbox}{wboxsep}{\setlength{\wboxsep}{#1}}
\define@key{wrdlbox}{wboxrcwd}{\setlength{\wboxrcwd}{#1}}
\define@key{wrdlbox}{wboxsize}{\setlength{\wboxsize}{#1}}
\define@key{wrdlbox}{wboxfontsize}{\def\wboxfontsize{#1}}
\define@key{wrdlbox}{mkwframe}[true]{\csname mkwframe#1\endcsname}
\define@key{wrdlbox}{wdrawbox}[true]{\csname wdrawbox#1\endcsname}
\define@key{wrdlbox}{wframefill}{\colorlet{wframefill}{#1}}
\makeatother

%% |=====8><-----| %%
%% keyval ootions: wboxsep, wboxrcwd, wboxsize, wboxfontsize, mkwframe, wframefill, wdrawbox
\NewDocumentEnvironment{wordframe}{O{}}{% six rows, six chains; more rows? just add more chains
    \setkeys{wrdlbox}{#1}%
    \setcounter{wrownum}{0}%
    \begin{tikzpicture}[%
        start chain=1 going right,
        start chain=2 going right,
        start chain=3 going right,
        start chain=4 going right,
        start chain=5 going right,
        start chain=6 going right,
        node distance=\wboxsep,outer sep=0pt]   
}{%
        \ifmkwframe
            \begin{scope}[on background layer]
                \draw[fill=wframefill,thick]
                    ([xshift=-\wboxsep,yshift=-\wboxsep]current bounding box.south west)
                    rectangle
                    ([xshift=\wboxsep,yshift=\wboxsep]current bounding box.north east);
            \end{scope}
        \fi
    \end{tikzpicture}%
}

\tikzset{wbox/.style={%
        text=white,
        font=\sffamily\bfseries\wboxfontsize,
        minimum size=\wboxsize,
        fill=\Clr,
        rounded corners=\wboxrcwd,
        \ifwdrawbox draw\fi
    }%
}

\def\dissect#1#2{% change colors to suit
    \def\Ltr{#1}%
    \if#2x
        \def\Clr{gray}% 'x' = gray = wrong letter
    \else
        \if#2!
            \def\Clr{green!80!blue}% '!' = green = right letter in right place
        \else
            \if#2?
                \def\Clr{yellow!80!orange}% '?' = yellow, right letter in wrong place
            \else
                \def\Clr{red}% incorrect letter tag, i.e., not "x" or "!" or "?"
            \fi
        \fi
    \fi 
}

\NewDocumentCommand{\wrow}{u{>}}{% comma-separated values: Letter/color; 'u' requires xparse.sty
    \stepcounter{wrownum}
    \foreach \X in {#1}{% TikZ foreach: Letter/color
        \expandafter\dissect\X
        \ifnum\thewrownum=1
            \node[wbox,on chain=\thewrownum]{\Ltr};
        \else
            \node[wbox,below=\wboxsep of \the\numexpr\thewrownum-1\relax-1,
                on chain=\thewrownum] {\Ltr};
        \fi
    }%
}

\catcode`<=\active
\let<=\wrow

\parindent0pt

\begin{document}

\begin{wordframe}
    <Gx,R!,A!,Ix,Lx>
    <Tx,R!,A!,C?,Kx>
    <C!,R!,A!,Mx,Px>
    <C!,R!,A!,Bx,Sx>
    <C!,R!,A!,Z!,Yx>
    <C!,R!,A!,Z!,E!>
\end{wordframe}

\bigskip

\noindent This is a frame \begin{wordframe}<Wx,Ox,R!,Dx,L?,E!>\end{wordframe} in a sentence.

\bigskip

\begin{wordframe}[wframefill=cyan!10,
    wboxsize=3em,wboxfontsize=\tiny,
    wboxsep=2mm,wboxrcwd=3pt,mkwframe]
    <Gx,R!,A!,Ix,Lx>
    <Tx,R!,A!,C?,Kx>
    <C!,R!,A!,Mx,Px>
    <C!,R!,A!,Bx,Sx>
    <C!,R!,A!,Z!,Yx>
    <C!,R!,A!,Z!,E!>
\end{wordframe}

\bigskip

\begin{wordframe}[wboxfontsize=\large,wboxsep=0pt,mkwframe,wdrawbox]
    <Gx,R!,A!,Ix,Lx>
    <Tx,R!,A!,C?,Kx>
    <C!,R!,A!,Mx,Px>
    <C!,R!,A!,Bx,Sx>
    <C!,R!,A!,Z!,Yx>
    <C!,R!,A!,Z!,E!>
\end{wordframe}

\end{document}

答案3

与其他答案非常相似,但您的问题让我想起了我的一条推文:https://twitter.com/CarLaTeXSE/status/1484979275598123015

\documentclass{standalone} 
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    mymatrix/.style={
        matrix of nodes,
        row sep=2pt,
        column sep=2pt,
        nodes={
            text width=10pt,
            text centered,
            text height=8pt,
            text depth=2pt
            }
        },
    greened/.style={fill=green},
    redded/.style={fill=red},
    yellowed/.style={fill=yellow},
    grayed/.style={fill=lightgray},
    }
\begin{document} 

\begin{tikzpicture}
\matrix[mymatrix] {
    |[grayed]|D&|[grayed]|U&|[grayed]|C&|[grayed]|K&|[grayed]|Y\\
    |[greened]|L&|[grayed]|I&|[grayed]|O&|[grayed]|N&|[grayed]|S\\
    |[greened]|L&|[yellowed]|E&|[yellowed]|A&|[grayed]|V&|[grayed]|E\\
    |[greened]|L&|[greened]|A&|[greened]|T&|[grayed]|I&|[grayed]|N\\
    |[greened]|L&|[greened]|a&|[greened]|T&|[greened]|e&|[greened]|X\\
    };
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案4

这是一个受@Qrrbrbirlbel 启发的解决方案回复我对他的帖子的评论。它定义了一个环境,,wordle它接受一个参数,这是 wordle 拼图的正确答案。然后,拼图的建议解决方案以单词的形式在环境主体中给出。例如,对于问题中的拼图,您可以这样写:

\begin{wordle}{CRAZE}
  GRAIL TRACK CRAMP CRABS CRAZY CRAZE
\end{wordle}

生产:

在此处输入图片描述

与上述许多解决方案一样,TikZ 用于打印字母,但大部分代码是用 LaTeX3 编写的。代码中有注释简要描述了其工作原理。

编辑此代码的初始版本无法正确处理单词中包含重复字母的情况。下面的更新版本可以正确处理这种情况。要做到这一点,必须“处理”每个单词两次,首先计算正确放置的字母数,然后才能正确地“标记”放置错误的重复字母。这使得代码稍微复杂一些,但并不可怕——尽管我承认,这是主观的。

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\tikzset{
  boxed letter/.style = {
    inner sep=1mm,
    rectangle,
    minimum width=4ex,
    minimum height=2ex,
    fill=#1,
    text=white,
    draw=white,
    thick
  }
}
\usepackage{expl3}
\ExplSyntaxOn

% print a letter in a box
\cs_new:Npn \l__boxed_letter:nn #1#2
{
  \tikz \node[boxed~letter=#1]{#2};
}

\seq_new:N \l_wordle_seq   % the answer
\prop_new:N \l_wordle_prop % count letters in wordle
\seq_new:N \l_words_seq    % list of all words
\prop_new:N \l_word_prop   % count letters in word
\int_new:N \l_letter_int   % index of current letter in word

% a conditional for nonnegative prop counter in \l_word_prop
\prg_new_protected_conditional:Npnn \if_wordle_letter_nonnegative:n #1 {TF}
{
    \prop_if_in:NnTF \l_word_prop {#1}
      { % letter in in prop
        \prop_get:NnN \l_word_prop {#1} \l_tmpa_tl
        \int_compare:nNnTF {\l_tmpa_tl} < {0}
          {\prg_return_false:} {\prg_return_true:}
      }
      {\prg_return_false:}
}

% a conditional for comparing letters
\prg_new_protected_conditional:Npnn \if_wordle_letters_agree:n #1 {T, TF}
{
    \str_set:Nx \l_tmpa_str {\seq_item:Nn \l_wordle_seq {\l_letter_int}}
    \str_if_eq:nVTF {#1} \l_tmpa_str {\prg_return_true:} {\prg_return_false:}
}

% add #3 to <prop=#1>.#2
\cs_new_protected:Npn \wordle__add_to_prop_counter:Nnn #1#2#3
{
    \prop_put_if_new:Nnn #1 {#2} {0}
    \prop_pop:NnN        #1 {#2} \l_tmp_a
    \prop_put:Nnx        #1 {#2} {\int_eval:n {#3+\l_tmp_a}}
}

% make \l_wordle_prop<x> = #x's in wordle_seq
\cs_new_protected:Npn \count_letters_in_wordle:n #1
{
    \wordle__add_to_prop_counter:Nnn \l_wordle_prop {#1} {1}
}

% first run: subtract correct matches from letter counts
\cs_new:Npn \count_letters_in_word:n #1
{
  \int_incr:N \l_letter_int
  \if_wordle_letters_agree:nT {#1}
     { \wordle__add_to_prop_counter:Nnn \l_word_prop {#1} {-1} }
}

% on the second run we print a coloured wordle word
\cs_new:Npn \write_letters_in_word:n #1
{
  \int_incr:N \l_letter_int
  \if_wordle_letters_agree:nTF {#1}
      { \l__boxed_letter:nn {ForestGreen}{#1} }
      {
         % subtract 1 from the prop counter
         \wordle__add_to_prop_counter:Nnn \l_word_prop {#1} {-1}
         % if the counter is non-negative this is a pseudo match
         \if_wordle_letter_nonnegative:nTF {#1}
            { \l__boxed_letter:nn {Goldenrod}{#1} }
            { \l__boxed_letter:nn {SlateGray}{#1} }
      }
}

\NewDocumentEnvironment{wordle}{ mb }
{
  \parindent=0pt
  % split the answer into letters
  \seq_set_split:Nnn \l_wordle_seq {} {#1}

  % we need to count the number of times each letter appears in \l_wordle_seq
  \prop_clear:N \l_wordle_prop
  \tl_map_function:nN {#1} \count_letters_in_wordle:n


  % split the solution into words
  \regex_split:nnN {\s} {#2} \l_words_seq


  % process the words
  \seq_map_inline:Nn \l_words_seq
  {
     \int_zero:N \l_letter_int
     \prop_set_eq:NN \l_word_prop \l_wordle_prop
     \tl_map_function:nN {##1} \count_letters_in_word:n
     \int_zero:N \l_letter_int
     \tl_map_function:nN {##1}  \write_letters_in_word:n
     \newline
  }

}{}

\ExplSyntaxOff

\begin{document}

\begin{wordle}{CRAZE}
  GRAIL TRACK CRAMP
  CRABS CRAZY CRAZE
\end{wordle}

\begin{wordle}{LILLY}
  TRACE LOUSY DIMLY HILLY FILLY LILLY
\end{wordle}

\begin{wordle}{LADLE}
   FLAIL LAPEL LADLE
\end{wordle}

\end{document}

除了上面的例子之外,代码还产生了两个谜题:

在此处输入图片描述

相关内容