Beamer 多项选择题风格的定义

Beamer 多项选择题风格的定义

对于在演示文稿中提出多项选择题,我想定义一些有助于突出显示答案的命令。答案可能应该跨越多行。

为此,目前一个可行的解决方案如下:

\documentclass[compress,aspectratio=169,12pt]{beamer}
\usepackage[utf8]{inputenc}

\newcommand{\correctAnswer}[1]{ \colorbox{green}{
        \hspace{-0.6cm}
    \begin{minipage}[t]{13cm}
      \hspace{0.3cm}
      #1
    \end{minipage}
    } }
\newcommand{\wrongAnswer}[1]{ \colorbox{red}{
        \hspace{-0.6cm}
    \begin{minipage}[t]{13cm}
      \hspace{0.3cm}
      #1
    \end{minipage}
    } }
\newcommand{\unknownAnswer}[1]{ \colorbox{white}{
        \hspace{-0.6cm}
    \begin{minipage}[t]{13cm}
      \hspace{0.3cm}
      #1
    \end{minipage}
    } }
    
\newcommand{\correctAnswerBlock}[1]{\alt<2>      
        {\unknownAnswer{#1}}
        {\correctAnswer{#1}}
    }
    
\newcommand{\wrongAnswerBlock}[1]{\alt<2>      
        {\unknownAnswer{#1}}
        {\wrongAnswer{#1}}
    }

\begin{document}

\begin{frame}[fragile]{Questions}
  \begin{onlyenv}<1>
    \begin{itemize}
      \item Are there questions from your side?
    \end{itemize}
  \end{onlyenv}
  \begin{onlyenv}<2-3>
    What is true regarding reductions?
    \begin{enumerate}
      \item \wrongAnswerBlock{If we can reduce a Problem $C$ to problem $D$ by a polynomial \par \hspace{0.3cm} algorithm $A_T$ ($C \leq_p D$), than both problems are in $\mathcal{P}$.}
      \item \wrongAnswerBlock{If we can reduce a Problem $D$ to problem $C$ by a polynomial \par \hspace{0.3cm} algorithm $A_T$ ($D \leq_p C$), than both problems are in $\mathcal{P}$.}
      \item \wrongAnswerBlock{Given a reduction $A_T$ of a problem $C$ to a problem $D$ ($C \leq_p D$), \par \hspace{0.3cm} we know that iff input $\alpha \in D$, than $A_T(\alpha) \in C$.}
      \item \correctAnswerBlock{Given a reduction $A_T$ of a problem $C$ to a problem $D$ ($C \leq_p D$), \par \hspace{0.3cm} we know that iff input $\alpha \in C$, than $A_T(\alpha) \in D$.}
    \end{enumerate}
  \end{onlyenv}
\end{frame}

\end{document}

这会产生正确的输出: 问题输出示例

不幸的是,它要求\par \hspace{0.3cm}为每个换行符添加,否则,minipage换行符会太靠左。不幸的是,添加负数hspace是必要的,否则,colorbox会插入一些空格: 没有负 hspace 的示例 此外,添加负面vspace(或\hspace*,如建议的那样迷你页面内没有水平间距) 并没有解决问题。

或者,也可以使用tcbox

\usepackage{tcolorbox}
\usepackage{varwidth}

\tcbset{colframe=white,
  colback=white,
  coltext=black,
  varwidth upper}
\newcommand{\correctAnswer}[1]{ \tcbox[colback=green, left=0cm, top=0cm, bottom=0cm]{#1} }  
\newcommand{\wrongAnswer}[1]{ \tcbox[colback=red, left=0cm, top=0cm, bottom=0cm]{#1} }
\newcommand{\unknownAnswer}[1]{ \tcbox[colback=white, left=0cm, top=0cm, bottom=0cm]{#1}}

这是可行的,但是会破坏项目的对齐:

tcolbox 示例

设置boxsep(例如在 tcolorbox 中设置行距)等参数并没有改变对齐的问题,而且halignvalign属性似乎都没有改变anchor。

即使经过一段时间的折腾,我还是找不到解决方案(除了\par \hspace{-0.6cm}解决方案,但不是很好)。有没有解决方案可以让我的文本周围出现一个彩色框,并在枚举内正确对齐?

答案1

您无需手动调整间距,而是可以更改\fboxsep

\documentclass[compress,aspectratio=169,12pt]{beamer}
\usepackage[utf8]{inputenc}

\setlength{\fboxsep}{0.3cm}

\newcommand{\correctAnswer}[1]{\colorbox{green}{%
    \begin{minipage}[t]{\dimexpr\linewidth-2\fboxsep}
      #1
    \end{minipage}%
    }}
\newcommand{\wrongAnswer}[1]{\colorbox{red}{%
    \begin{minipage}[t]{\dimexpr\linewidth-2\fboxsep}
      #1
    \end{minipage}%
    }}
\newcommand{\unknownAnswer}[1]{\colorbox{white}{%
    \begin{minipage}[t]{\dimexpr\linewidth-2\fboxsep}
      #1
    \end{minipage}%
    }}
    
\newcommand{\correctAnswerBlock}[1]{\alt<2>      
        {\unknownAnswer{#1}}
        {\correctAnswer{#1}}
    }
    
\newcommand{\wrongAnswerBlock}[1]{\alt<2>      
        {\unknownAnswer{#1}}
        {\wrongAnswer{#1}}
    }

\begin{document}

\begin{frame}[fragile]{Questions}
  \begin{onlyenv}<1>
    \begin{itemize}
      \item Are there questions from your side?
    \end{itemize}
  \end{onlyenv}
  \begin{onlyenv}<2-3>
    What is true regarding reductions?
    \begin{enumerate}
      \item \wrongAnswerBlock{If we can reduce a Problem $C$ to problem $D$ by a polynomial \par  algorithm $A_T$ ($C \leq_p D$), than both problems are in $\mathcal{P}$.}
      \item \wrongAnswerBlock{If we can reduce a Problem $D$ to problem $C$ by a polynomial \par algorithm $A_T$ ($D \leq_p C$), than both problems are in $\mathcal{P}$.}
      \item \wrongAnswerBlock{Given a reduction $A_T$ of a problem $C$ to a problem $D$ ($C \leq_p D$), \par  we know that iff input $\alpha \in D$, than $A_T(\alpha) \in C$.}
      \item \correctAnswerBlock{Given a reduction $A_T$ of a problem $C$ to a problem $D$ ($C \leq_p D$), \par  we know that iff input $\alpha \in C$, than $A_T(\alpha) \in D$.}
    \end{enumerate}
  \end{onlyenv}
\end{frame}

\end{document}

在此处输入图片描述

答案2

我不会\tcbox为此使用 a。如果你改用 a tcolorbox,则所有项目都会具有相同的宽度,并且你不必手动强制换行:

\documentclass[compress,aspectratio=169,12pt]{beamer}

\usepackage[most]{tcolorbox}

\tcbset{
  colframe=white,
  colback=white,
  coltext=black,
  }

\newtcolorbox{answerbox}[1][]{left=0.3cm, right=0.3cm, top=0cm, bottom=0cm,box align=top,nobeforeafter,before skip=0pt,#1,ignore nobreak}

\colorlet{wrongcol}{red}
\colorlet{correctcol}{green}
  
\newcommand{\correctAnswer}[1]{%
  \only<2>{\colorlet{correctcol}{white}}%
  \raisebox{.87\baselineskip}{\begin{answerbox}[colback=correctcol]
    #1
  \end{answerbox}}%
} 
\newcommand{\wrongAnswer}[1]{%
  \only<2>{\colorlet{wrongcol}{white}}%
  \raisebox{.87\baselineskip}{\begin{answerbox}[colback=wrongcol]
    #1
  \end{answerbox}}%
}
\newcommand{\unknownAnswer}[1]{%
  \raisebox{.87\baselineskip}{\begin{answerbox}[colback=white]
    #1
  \end{answerbox}}%
}


\begin{document}

\begin{frame}[fragile]{Questions}
  \begin{onlyenv}<1>
    \begin{itemize}
      \item Are there questions from your side?
    \end{itemize}
  \end{onlyenv}
  \begin{onlyenv}<2-3>
    What is true regarding reductions?
    \begin{enumerate}
      \item \wrongAnswer{If we can reduce a Problem $C$ to problem $D$ by a polynomial algorithm $A_T$ ($C \leq_p D$), than both problems are in $\mathcal{P}$.}
      \item \wrongAnswer{If we can reduce a Problem $D$ to problem $C$ by a polynomial  algorithm $A_T$ ($D \leq_p C$), than both problems are in $\mathcal{P}$.}
      \item \wrongAnswer{Given a reduction $A_T$ of a problem $C$ to a problem $D$ ($C \leq_p D$),   we know that iff input $\alpha \in D$, than $A_T(\alpha) \in C$.}
      \item \correctAnswer{Given a reduction $A_T$ of a problem $C$ to a problem $D$ ($C \leq_p D$),   we know that iff input $\alpha \in C$, than $A_T(\alpha) \in D$.}
    \end{enumerate}
  \end{onlyenv}
\end{frame}

\end{document}

在此处输入图片描述

相关内容