最小不工作示例

最小不工作示例

我有一个名为的外部文件,input.txt由逗号分隔的行组成,如下所示。注意:行也可能包含日语字符。

i, love, LaTeX, very, much
i, am, a, student

我想制作一张拼图纸,上面打印着随机的行,但每行左侧都有一个计数数字。更准确地说,每行的单词都是随机的。行与行之间没有随机性。

只有选择 时show-answer=true,原行才会以红色显示。

最小不工作示例

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents*}{input.txt}
i, love, LaTeX, very, much
i, am, a, student
\end{filecontents*}

\begin{document}
\Randomize[show-answer=true]{input.txt}
\end{document}

评论:

如果show-answer=true\Randomize产生以下

\begin{enumerate}
    \item  LaTeX, i, very, love, much \\
                \textcolor{red}{i, love, LaTeX, very, much}
    \item i, student, a, am \\
                \textcolor{red}{i, am, a, student}
\end{enumerate}

如果show-answer=false\Randomize产生以下

\begin{enumerate}
    \item  LaTeX, i, very, love, much 
    \item i, student, a, am 
\end{enumerate}

答案1

在此处输入图片描述

\documentclass{article}
\usepackage{filecontents,xcolor}
\usepackage{luacode}

\begin{luacode*}

        tp = tex.print
        local questions = {}
        local answers = {}

    function file_exists(name)
        local f=io.open(name,"r")
        if f~=nil then io.close(f) return true else return false end
        end

    function Puzzle ( name )
        local f,i,j
        if not file_exists (name)
        then
        tex.print ("Le fichier "..name.." n''existe pas")
        else
            out = io.open( name , 'r' )
            t = out:read("*all")
            io.close(out)
            --% slpit the file in lines
            for i in t:gmatch("([^\n]+)")
            do
                local line = {}
                local line_q
                --% split the lines in words
                for j in i:gmatch("([^%p]+)")
                do
                    j = j:gsub("%s","")
                    table.insert ( line , j )
                end
                --% store the answer
                table.insert ( answers , i )
                --% make and store the question
                line_q = ""
                while line[1] ~= nil
                do
                line_q = line_q
                    ..table.remove(line, math.random(1,#line))
                    ..", "
                end
                line_q = line_q:sub(1,-3)
                table.insert(questions , line_q)
            end
        end 
    end

    function Questions ( rep )
        local i,j
        tp ("\\begin{enumerate}")
            for i,j in ipairs(questions)
            do
                tp ("\\item "..j)
                if rep
                then
                    tp ("\\\\\\textcolor{red}{"..answers[i].."}")
                end
            end
        tp ("\\end{enumerate}")
    end

\end{luacode*}

\newcommand{\Questions}[1]{%
    \luadirect{
        Puzzle("#1")
        Questions()
    }
}

\newcommand{\Answers}{%
    \luadirect{
        Questions(1)
    }
}


\begin{filecontents*}{input.txt}
i, love, LaTeX, very, much
i, am, a, student
\end{filecontents*}

\begin{document}

\textbf{Questions}
\Questions{input.txt}

\bigskip

\textbf{Answers}
\Answers
\end{document}

相关内容