I'm having a really hard time understanding this error in a massive document. I took the time to work out a "minimal example", so I could post the error with precision. Please, when trying to answer, do not change the structure, assuming this "toy example" could be rearranged easily. Thanks a lot. Here the example. Bellow it I would explain more about the attempts.
% By Dr. Beco - 60 games
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc} % accented chars
\usepackage[english]{babel} % translations
\usepackage{graphicx} % includegraphics
\usepackage[ps]{skak} % chess
\usepackage{float} % figure[H]
\usepackage{xstring} % get tokens
%%%%%%%%%%%%%%%%%%%%% define \variable = value
\newcommand{\setvalue}[2]
{
\ifdefined #1
\renewcommand{#1}{#2}
\else
\newcommand{#1}{#2}
\fi
}
%%%%%%%%%%%%%%%%%%%%% fill all needed variables for a section
\newcommand{\setsec}[6]
{
\setvalue{\gnumber}{#1}
\setvalue{\gcolor}{#2}
\setvalue{\gdate}{#3}
\setvalue{\gresult}{#4}
\setvalue{\gopening}{#5}
\setvalue{\gfirstmoves}{#6}
\mysec{\gnumber}{\gcolor}{\gdate}{\gresult}{\gopening}{#6}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%% inicia seção
% parâmetros:
% #1 num. jogo
% #2 minha cor (W ou B)
% #3 Data
% #4 resultado (1:brancas, 0:pretas, 2:empate)
% #5 ECO code seguido do nome da abertura
% #6 Linha principal para mostrar a abertura
\newcommand{\mysec}[6]
{
\section*{Code #1#2, date: #3 result: #4 opening: #5}
\mygamesi{#1}{#2}{#5}{#6}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \mygamesi{Game Number}{Color}{Opening}{Moves}
\newcommand{\mygamesi}[4]
{
\newgame
\mainline{#4}
\begin{figure}[H]
\centering
\showboard
\caption{#1#2, #3}
\label{g1}
\end{figure}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% show some variation on board
% #1 game number \gnumber
% #2 my color \gcolor
% #3 #2 label key for \ref{}
% #4 #3 variation to show (board and caption)
% optional #1 : s=show full var in caption, h=hide, only last move in caption
\newcommand{\myvar}[3][h]
{
\storegame{mainline}
\hidemoves{#3}
\begin{figure}[H]
\centering
\showboard
\ifthenelse{\equal{#1}{s}}{\caption{opt S:position after variation \protect\movecomment{#3}}}{}
\ifthenelse{\equal{#1}{h}}{\caption{opt H:position after variation \protect\movecomment{\protect\StrBefore[2]{#3}{ }} ... \lastmove{}}}{} %<<<<<<<<<<<<<<<<<
\label{gv}
\end{figure}
\restoregame{mainline}
}
%%%%%%%%%%%%%%%%%%%%%%%%%% fim de seção
% #1 num. jogo \gnumber
% #2 minha cor (W ou B) \gcolor
\newcommand{\myend}
{
\begin{figure}[H]
% \includegraphics{\gnumber-graph.png}
[altern. text: evaluation graph]
\caption{\gnumber\gcolor, evaluation graph}
\label{ge}
\end{figure}
}
\tinyboard
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\setsec{1}{B}{2017.17.17}{0}{B00 Strange Defense}{1. e3 e5 2. d3 d4 3. Nc3 Bb4}
It was a serious game.
\mainline{4. a3 Bxc3+ 5. bxc3 dxe4 6. Qg4 Nf6 7. Qxg7
Rg8 8. Qh6 c5 9. Bg5 Ng4 }
after \variationcurrent{10. Qxh7 Qxg5 11. Qxe4 Nf6 12. Bb5+ Nbd7} you can see:
my var show:
\myvar[s]{v1}{10. Qxh7 Qxg5 11. Qxe4 Nf6 12. Bb5+ Nbd7}
my var hide:
\myvar[h]{v1}{10. Qxh7 Qxg5 11. Qxe4 Nf6 12. Bb5+ Nbd7}
White resigns
\myend
\end{document}
- Note: We are using PDFLatex
- The line with error is marked with
%<<<<<<<<<<<<<<<<<
What I expect it to do
\myvar[s]
would show all chess moves inside the caption. That is working fine.\myvar[h]
would show just the first move of the string, 3 dots, and the last move. That is unachievable.
The problem is that I can't give the skak
command \movecomment{}
a variable inside caption.
I've tried all combinations of \def
and \edef
, but I don't really understand those, so I prefer a LaTeX
approach. I've tried \newcommand
and DeclareRobustCommand
also, and adding and removing \protect
in many places on the way.
The idea is very simple: given a string with moves, say "1. e4 e5 2. d4 d5", I want the "first move", that is "1. e4". The skak
package gives a lastmove
that works inside captions, I even read its code. But I can't understand it to adapt.
My simple solution would be:
For example:
\StrBefore[2]{#1}{ }[\firstmove]
and then
\movecomment{\firstmove}
But who says this works? I've being trying and reading about this expansions and macros for sometime now, but I can't work it out.
Thanks any help.
Edited:
Also something that isn't working:
\newcommand{\setsec}[6]
{
\setvalue{\gnumber}{#1}
\setvalue{\gcolor}{#2}
\setvalue{\gdate}{#3}
\setvalue{\gresult}{#4}
\setvalue{\gopening}{#5}
\setvalue{\gfirstmoves}{#6}
\mysec{\gnumber}{\gcolor}{\gdate}{\gresult}{\gopening}{#6}
}
The last line should really be:
\mysec{\gnumber}{\gcolor}{\gdate}{\gresult}{\gopening}{\gfirstmoves}
but alas...
Current solution (workaround)
Inside command
\myvar
:\ifthenelse{\equal{#1}{s}}{\caption{THEN: position after variation \protect\movecomment{#3}}}{\caption{ELSE: position after variation \protect\movecomment{#1} \ldots\ \lastmove{}}}
If the whole line is to be in caption:
\myvar[s]{v1}{10. Qc4+ Kb8 11. Qd3 Qf6 12. Rd1 Qxf2} % or \myvar{v1}{10. Qc4+ Kb8 11. Qd3 Qf6 12. Rd1 Qxf2}
If only first and last move is to be in caption:
\myvar[22. Qc4+]{v1}{10. Qc4+ Kb8 11. Qd3 Qf6 12. Rd1 Qxf2}
Edited: removed package multicolumn - not a minimum example after all. ;)
\mysec
and\myend
to enclose\begin{}
and\end{}
calls. I cannot help but believe that somewhere this approach will cause a crash. My understanding is that a better approach will be obtained by generating environments where the beginning and ending are not separated (and not themselves called within\newcommand
). Have you tried using the built-in\newenvironment
command or using commands in\usepackage{environ}
?\newcommand
functions that require so many parameters all at once and then pass them to other\newcommand
functions that themselves perhaps need yet other parameters. For LaTeX, avoid using approaches that are known to break LaTeX in even basic cases.\StrBefore[2]{#1}{ }[\firstmove]
where#1
is the string1. e4 e5 2. d4 d5
and\movecomment{\firstmove}
inside a caption. Any help is appreciated. Try to compile the code and, of course, you can change the code. Change any function. Any ideas I can get may help me to adapt it.