具有超过 9 个参数的宏

具有超过 9 个参数的宏

请考虑以下示例:

\documentclass{article}

\usepackage[
  a4paper,
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}        % https://ctan.org/pkg/geometry
\usepackage{xskak} % https://ctan.org/pkg/xskak

\newcommand*\hestA[2][\svarstedA]{%
  \underline{\makebox[\ifdim\width>#1\width\else#1\fi]{#2}}}
\newcommand*\opgaverLoesningerA[1]{\hestA{\textcolor{blue}{#1}}}
\newcommand*\opgaverLoesningerEkstra[6]{%
  \def\tempA{#1}%
  \def\tempB{#2}%
  \def\tempC{#3}%
  \def\tempD{#4}%
  \def\tempE{#5}%
  \def\tempF{#6}%
  \opgaverLoesninger
}
\newcommand*\opgaverLoesninger[6]{%
\begin{figure}[!htbp]
 \centering
  \begin{minipage}{0.5\textwidth}
   \centering
    \newgame
    \fenboard{\tempB \tempA - - 0 1}%
    \scalebox{1.2}{\showboard}%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{\tempC}}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}
   \centering
    \newgame
    \fenboard{\tempE \tempD - - 0 1}%
    \scalebox{1.2}{\showboard}%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{\tempF}}%
  \end{minipage}
\end{figure}
\vspace*{2ex}

\begin{figure}[!htbp]
 \centering
  \begin{minipage}{0.5\textwidth}
   \centering
    \newgame
    \fenboard{#2 #1 - - 0 1}%
    \scalebox{1.2}{\showboard}%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{#3}}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}
   \centering
    \newgame
    \fenboard{#5 #4 - - 0 1}%
    \scalebox{1.2}{\showboard}%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{#6}}%
  \end{minipage}
\end{figure}}

\begin{document}

\opgaverLoesninger%
  {w}{6k1/6p1/1p5p/3r4/1P6/6P1/4B1KP/8}{Lc4}%
  {w}{1k6/pp6/2p3p1/4n1P1/8/P6P/KP1B4/8}{Lf4}%
  {w}{8/1p4kp/p5p1/B7/3n4/1P5P/2P3P1/6K1}{Lc3}%
  {w}{k7/p6p/1p4p1/3r4/1P5P/P5P1/1KB5/8}{Le4}

\end{document}

当我使用编译时xelatex,出现错误

! Paragraph ended before \FenBoard was complete.
<to be read again> 
                   \par 
l.73   {w}{1k6/pp6/2p3p1/4n1P1/8/P6P/KP1B4/8}{Lf4}
                                                  %

有人能帮我解决这个问题吗?

PS 我认为这与我尝试在宏中使用超过 9 个参数(这里是 12 个参数)有关,但我不确定。

答案1

那如果要画五块板呢?或者八块呢?

这是一个接受的实现任何板数,并在环境中成对输出figure。如果数量为奇数,则最后一个板会在另一个环境中单独输出figure

\documentclass{article}
\usepackage[
  a4paper,
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{xskak}

\setchessboard{smallboard}

\ExplSyntaxOn

\NewDocumentCommand{\opgaverLoesninger}{m}
 {
  \svend_opgaver_loesninger:n { #1 }
 }

\tl_new:N \l__svend_opgaver_final_tl
\seq_new:N \l__svend_opgaver_tables_in_seq
\seq_new:N \l__svend_opgaver_tables_out_seq

\cs_new_protected:Nn \svend_opgaver_loesninger:n
 {
  \seq_set_split:Nnn \l__svend_opgaver_tables_in_seq { \\ } { #1 }
  \seq_clear:N \l__svend_opgaver_tables_out_seq
  \seq_map_inline:Nn \l__svend_opgaver_tables_in_seq
   {
    \seq_put_right:Nx \l__svend_opgaver_tables_out_seq
     {
      \__svend_opgaver_board:nn
       { \clist_item:nn { ##1 } { 1 } } 
       { \clist_item:nn { ##1 } { 2 } } 
      \__svend_opgaver_caption:n { \clist_item:nn { ##1 } { 3 } }
     }
   }
  \__svend_opgaver_make_tables:
 }

\cs_new_protected:Nn \__svend_opgaver_board:nn
 {
  \newgame
  \fenboard{#2 ~ #1 ~ - ~ - ~ 0 ~ 1 }
 }

\cs_new_protected:Nn \__svend_opgaver_caption:n
 {
  \scalebox{1.2}{\showboard}\par
  \vspace*{2ex}
  {\LARGE Løsning: ~ \textcolor{blue}{#1}}
 }

\cs_new_protected:Nn \__svend_opgaver_make_tables:
 {
  \tl_clear:N \l__svend_opgaver_final_tl
  \int_if_odd:nT { \seq_count:N \l__svend_opgaver_tables_out_seq }
   {
    \seq_pop_right:NN \l__svend_opgaver_tables_out_seq \l__svend_opgaver_final_tl
   }
  \int_step_inline:nnnn { 1 } { 2 } { \seq_count:N \l__svend_opgaver_tables_out_seq }
   {
    \begin{figure}[!htbp]
    \begin{minipage}[b]{0.5\textwidth}
    \centering
    \seq_item:Nn \l__svend_opgaver_tables_out_seq { ##1 }
    \end{minipage}
    \begin{minipage}[b]{0.5\textwidth}
    \centering
    \seq_item:Nn \l__svend_opgaver_tables_out_seq { ##1+1 }
    \end{minipage}
    \end{figure}
   }
   \tl_if_empty:NF \l__svend_opgaver_final_tl
   {
    \begin{figure}[!htbp]
    \centering
    \tl_use:N \l__svend_opgaver_final_tl
    \end{figure}
   }
 }

\ExplSyntaxOff

\begin{document}

\opgaverLoesninger{
  w, 6k1/6p1/1p5p/3r4/1P6/6P1/4B1KP/8, Lc4 \\
  w, 1k6/pp6/2p3p1/4n1P1/8/P6P/KP1B4/8, Lf4 \\
  w, 8/1p4kp/p5p1/B7/3n4/1P5P/2P3P1/6K1, Lc3 \\
  w, k7/p6p/1p4p1/3r4/1P5P/P5P1/1KB5/8, Le4 \\
  w, k7/p6p/1p4p1/3r4/1P5P/P5P1/1KB5/8, Le4
}

\end{document}

与使用括号的语法相比,用\\逗号分隔板和每个板中的数据更简单且更具可读性。

在此处输入图片描述

在此处输入图片描述

稍加修改,您还可以设置板的放大倍数(默认 1.2)和每行的板数(默认 2)。

放大倍数用 改变mag=<factor>,每行的板数用 的n=<number>可选参数设置\opgaverLoesninger

该选项single(布尔值,single=true隐式)将创建单个图形环境,但您当然要负责使其适合页面。它出现在示例中只是为了举例。如果您尝试\opgaverLoesninger[single]{...}(五个板的相同强制参数),浮动将太大(四个板则不行)。

\documentclass{article}
\usepackage[
  a4paper,
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{xskak}

\setchessboard{smallboard}

\ExplSyntaxOn

\NewDocumentCommand{\opgaverLoesninger}{O{}m}
 {
  \keys_set:nn { svend/opgaver } { n=2, mag=1.2, single=false, #1 }
  \svend_opgaver_loesninger:n { #2 }
 }

\keys_define:nn { svend/opgaver }
 {
  n      .int_set:N  = \l__svend_opgaver_n_int,
  mag    .tl_set:N   = \l__svend_opgaver_mag_tl,
  single .bool_set:N = \l__svend_opgaver_single_bool,
  single .default:n  = true,
 }

\tl_new:N \l__svend_opgaver_final_tl
\seq_new:N \l__svend_opgaver_tables_in_seq
\seq_new:N \l__svend_opgaver_tables_out_seq

\cs_new_protected:Nn \svend_opgaver_loesninger:n
 {
  \seq_set_split:Nnn \l__svend_opgaver_tables_in_seq { \\ } { #1 }
  \seq_clear:N \l__svend_opgaver_tables_out_seq
  \seq_map_inline:Nn \l__svend_opgaver_tables_in_seq
   {
    \seq_put_right:Nx \l__svend_opgaver_tables_out_seq
     {
      \__svend_opgaver_board:nn
       { \clist_item:nn { ##1 } { 1 } } 
       { \clist_item:nn { ##1 } { 2 } } 
      \__svend_opgaver_caption:n { \clist_item:nn { ##1 } { 3 } }
     }
   }
  \__svend_opgaver_make_tables:
 }

\cs_new_protected:Nn \__svend_opgaver_board:nn
 {
  \newgame
  \fenboard{#2 ~ #1 ~ - ~ - ~ 0 ~ 1 }
 }

\cs_new_protected:Nn \__svend_opgaver_caption:n
 {
  \scalebox{\l__svend_opgaver_mag_tl}{\showboard}\par
  \vspace*{2ex}
  {\LARGE Løsning: ~ \textcolor{blue}{#1}}
 }

\cs_new_protected:Nn \__svend_opgaver_make_tables:
 {
  \bool_if:NT \l__svend_opgaver_single_bool { \begin{figure}[!htbp] \centering }
  \int_step_inline:nnnn
   { 1 } % start
   { \l__svend_opgaver_n_int } % step
   { \seq_count:N \l__svend_opgaver_tables_out_seq } % end
   {
    \bool_if:NF \l__svend_opgaver_single_bool { \begin{figure}[!htbp] \centering }
    \int_step_inline:nnnn { 0 } { 1 } { \l__svend_opgaver_n_int - 1 }
     {
      \tl_if_empty:xF
       { \seq_item:Nn \l__svend_opgaver_tables_out_seq { ##1+####1 } }
       {
        \begin{minipage}[b]{ \dim_eval:n { \textwidth/\l__svend_opgaver_n_int } }
        \centering
        \seq_item:Nn \l__svend_opgaver_tables_out_seq { ##1+####1 }
        \end{minipage}
       }
     }
    \bool_if:NTF \l__svend_opgaver_single_bool { \\ \bigskip } { \end{figure} }
   }
  \bool_if:NT \l__svend_opgaver_single_bool { \end{figure} }
 }
\cs_generate_variant:Nn \tl_if_empty:nF { x }

\ExplSyntaxOff

\begin{document}

\opgaverLoesninger[mag=0.5,n=4,single]{
  w, 6k1/6p1/1p5p/3r4/1P6/6P1/4B1KP/8, Lc4 \\
  w, 1k6/pp6/2p3p1/4n1P1/8/P6P/KP1B4/8, Lf4 \\
  w, 8/1p4kp/p5p1/B7/3n4/1P5P/2P3P1/6K1, Lc3 \\
  w, k7/p6p/1p4p1/3r4/1P5P/P5P1/1KB5/8, Le4 \\
  w, k7/p6p/1p4p1/3r4/1P5P/P5P1/1KB5/8, Le4
}

\end{document}

在此处输入图片描述

答案2

我认为您或多或少正在寻找类似的东西(我禁用 \hestA 命令,因为维度\svarstedA没有定义)。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[
  a4paper,
  hmargin = 2.4cm,
  vmargin = 3cm
]{geometry}
\usepackage{xskak}

\setchessboard{smallboard}

\newcommand*\hestA[2][\svarstedA]{%
  \underline{\makebox[\ifdim\width>#1\width\else#1\fi]{#2}}}

\newcommand*\opgaverLoesningerA[1]{{\textcolor{blue}{#1}}}
\newcommand*\opgaverLoesningerEkstra[6]{%
  \def\tempA{#1}%
  \def\tempB{#2}%
  \def\tempC{#3}%
  \def\tempD{#4}%
  \def\tempE{#5}%
  \def\tempF{#6}%
  \opgaverLoesninger
}

\newcommand*\opgaverLoesninger[6]{%
\begin{figure}[!htbp]
 \centering
  \begin{minipage}{0.5\textwidth}
   \centering
    \newchessgame[setfen=\tempB\space \tempA - - 0 1,print]%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{\tempC}}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}
   \centering
    \newchessgame[setfen=\tempE \space \tempD - - 0 1,print]%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{\tempF}}%
  \end{minipage}
\end{figure}
\vspace*{2ex}

\begin{figure}[!htbp]
 \centering
  \begin{minipage}{0.5\textwidth}
   \centering
    \newchessgame[setfen = #2 #1 - - 0 1,print]%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{#3}}%
  \end{minipage}%
  \begin{minipage}{0.5\textwidth}
   \centering
    \newchessgame[setfen= #5 #4 - - 0 1,print]%
    \vspace*{2ex}

    {\LARGE Løsning: \opgaverLoesningerA{#6}}%
  \end{minipage}
\end{figure}}

\begin{document}

\opgaverLoesningerEkstra%
  {w}{6k1/6p1/1p5p/3r4/1P6/6P1/4B1KP/8}{Lc4}%
  {w}{1k6/pp6/2p3p1/4n1P1/8/P6P/KP1B4/8}{Lf4}%
  {w}{8/1p4kp/p5p1/B7/3n4/1P5P/2P3P1/6K1}{Lc3}%
  {w}{k7/p6p/1p4p1/3r4/1P5P/P5P1/1KB5/8}{Le4}

\end{document}

在此处输入图片描述

相关内容