自定义问题环境的改进

自定义问题环境的改进

我正在遵循自定义的问题环境,该环境以页面右侧问题末尾的空白方块结束。

如果问题以一些文本而不是数学环境结尾,则效果很好。但是,如果问题以数学环境结尾,则数学环境的最后一行和结束方块之间会出现一个额外的空格。

最小工作示例是

\documentclass[oneside]{book}
\usepackage[T1]{fontenc}
\usepackage{graphicx,color, xcolor}
\usepackage{amsmath, amssymb, amsthm}

\newcounter{ques}[chapter]

\newenvironment{question}[2][Question \stepcounter{ques}\arabic{ques}:]{\begin{trivlist}
        \item[\hskip \labelsep \bfseries #1] #2 \par \textbf{Answer:}} {\hfill{$\square$} \end{trivlist} }
    
\begin{document}
\begin{question}{Find $\Sigma X$, if $\overline{X}=15$, $n=10$}
    \begin{align*}
        \overline{X} &=\frac{\Sigma X}{n}\\
        15 & = \frac{\Sigma X}{10} \\
        \Rightarrow \Sigma X & = 15\times 10=150
    \end{align*}
\end{question}

\begin{question}{The mean of 10 numbers is 8. If an eleventh number is added, the mean becomes 9. What is the eleventh value?}
    \begin{align*}
        \text{Mean of 10 numbers } &= 8\\
        \text{Mean of 11 numbers } &= 9\\
        \text{Total of 10 numbers} &= 80\\
        \text{Total of 11 numbers} &= 99\\
        \therefore \text{value of eleventh number} &= 99-88 = 19
    \end{align*}
So, the value of eleventh number is = 199
\end{question}
\end{document}

在此处输入图片描述

需要宝贵的意见来改善定制问题环境

答案1

如果可以使用LuaTeX的话,那么很容易在Lua端实现一些模式匹配和字符串操作函数来实现这一点。

为了使代码简短,我将操作后的字符串写入文件系统,并让 LaTeX 从中读取。如果问题环境数量很大,这可能会很慢。如果有必要,有一种方法可以让 LuaTeX\input从内存中读取。有关更多信息,请参阅这里

\documentclass[oneside]{book}
\usepackage[T1]{fontenc}
\usepackage{graphicx,color, xcolor}
\usepackage{amsmath, amssymb, amsthm}
\usepackage{luacode}
\usepackage{xparse}

\begin{luacode*}
local spacer = lpeg.S(" \t\f\v")
local nonspacer = 1 - spacer
local stripper = spacer^0 * lpeg.C((spacer^0 * nonspacer^1)^0)

function strip(str)
    return str and lpeg.match(stripper, str) or ""
end

verb_table = {}

function store_lines(str)
  if string.find (str , "\\end{question}" ) then
    luatexbase.remove_from_callback (
      "process_input_buffer" , "store_lines")
      return "\\end{question}"
  else
    table.insert(verb_table, str)
  end
  return ""
end

function register_verbatim()
  verb_table = {}
  luatexbase.add_to_callback(
    "process_input_buffer" , store_lines , "store_lines")
end

local math_end = {
  ["\\end{align*}"] = 1,
  ["\\end{gather*}"] = 1,
  ["\\end{equation*}"] = 1,
  ["\\end{multline*}"] = 1,
}

function process_verbatim()
  local non_empty_lines = {}
  for ind, str in ipairs(verb_table) do
    str = strip(str)
    if str:len() > 0 then
      table.insert(non_empty_lines, ind)
    end
  end
  local last_line_ind = non_empty_lines[#non_empty_lines]
  local last_line = verb_table[last_line_ind]
  if math_end[strip(last_line)] ~= nil then
    -- make sure second to last line exists
    local last_two_line_ind = non_empty_lines[#non_empty_lines - 1]
    local last_two_line = verb_table[last_two_line_ind]
    assert(last_two_line ~= nil, "invalid math environment")
    last_two_line = last_two_line .. "\\tag*{$\\square$}"
    verb_table[last_two_line_ind] = last_two_line
  else
    last_line = last_line .. "\\hfill{$\\square$}"
    verb_table[last_line_ind] = last_line
  end
  local out_str = table.concat(verb_table, "\n")
  local file = io.open(tex.jobname .. ".tmp", "w")
  file:write(out_str)
  file:close()
end

\end{luacode*}

\newcounter{ques}[chapter]
\NewDocumentEnvironment{question}{O{Question \stepcounter{ques}\arabic{ques}:}m}{
\begin{trivlist}
\item[\hskip \labelsep \bfseries #1] #2 \par \textbf{Answer:}
\directlua{register_verbatim()}
}{
\directlua{process_verbatim()}
\input{\jobname.tmp}
\end{trivlist}
}

\begin{document}
\begin{question}{Find $\Sigma X$, if $\overline{X}=15$, $n=10$}
    \begin{align*}
        \overline{X} &=\frac{\Sigma X}{n}\\
        15 & = \frac{\Sigma X}{10} \\
        \Rightarrow \Sigma X & = 15\times 10=150
    \end{align*}
\end{question}

\begin{question}{The mean of 10 numbers is 8. If an eleventh number is added, the mean becomes 9. What is the eleventh value?}
    \begin{align*}
        \text{Mean of 10 numbers } &= 8\\
        \text{Mean of 11 numbers } &= 9\\
        \text{Total of 10 numbers} &= 80\\
        \text{Total of 11 numbers} &= 99\\
        \therefore \text{value of eleventh number} &= 99-88 = 19
    \end{align*}
So, the value of eleventh number is = 199
\end{question}
\end{document}

相关内容