Skip to content

Instantly share code, notes, and snippets.

@James-Ansley
Last active November 27, 2022 06:28
Show Gist options
  • Save James-Ansley/53ac2c34fea563cf24324251feba92f8 to your computer and use it in GitHub Desktop.
Save James-Ansley/53ac2c34fea563cf24324251feba92f8 to your computer and use it in GitHub Desktop.
Simple LaTeX Question package

A simple LaTeX package that conditionally renders questions and solutions in pretty boxes. Problems and problem solutions can be added to the same document and toggled with the boolean optional params problems and solutions.

Provides numbered problem and solution environments. Both environemnts take a question title as a parameter.

Also provides \problemsec{} and \solutionsec{} commands that create sections divided by bars within the problem and solution boxes; each command takes a subsection heading as a parameter.

For example, the following:

\documentclass[10pt]{article}
\usepackage{minted}

\usepackage[problems,solutions]{questions}
% Change this to:
% \usepackage[problems]{questions} % for problem-only rendering
% \usepackage[solutions]{questions} % for solution-only rendering

\begin{document}


\begin{problem}[Greeting]

Write a function called \mintinline{python}{hello} that takes a name 
(\mintinline{python}{str}) as a parameter.
The function should print ``Hello, [name]!''

\problemsec{For Example:}

\begin{minted}{python}
>>> hello("James")
# Hello, James!
\end{minted}

\end{problem}


\begin{solution}[Greeting]

\begin{minted}{python}
def hello(name: str):
    print(f"Hello, {name}!")
\end{minted}

\solutionsec{Explanation:}

f-strings (strings that start with the letter f) can be used to fill placeholders 
(surrounded by curly braces) with variables.

\end{solution}


\end{document}

Would produce:

image

Loading the questions package as \usepackage[problems]{questions} would only render the problems. Conversely, loading \usepackage[solutions]{questions} would only render the solutions.

Note: I have had some issues with indentation – Make sure the \begin and \end commands for the problem and solution environments are NOT indented.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{questions}
\RequirePackage{tcolorbox}
\RequirePackage{comment}
\RequirePackage{kvoptions}
\DeclareBoolOption{problems}
\DeclareBoolOption{solutions}
\ProcessKeyvalOptions*
\newcommand{\problemsec}{\tcbsubtitle}
\newcommand{\solutionsec}{\tcbsubtitle}
\ifquestions@problems
\newtcolorbox[auto counter]{problem}[1][]
{
title=\textbf{Problem~\thetcbcounter:} #1,
subtitle style={boxrule=0.5pt,colback=black!60!white},
parbox=false,
}
\else
\excludecomment{problem}
\fi
\ifquestions@solutions
\newtcolorbox[auto counter]{solution}[1][]
{
title=\textbf{Solution~\thetcbcounter:} #1,
subtitle style={boxrule=0.5pt,colback=black!60!white},
parbox=false,
}
\else
\excludecomment{solution}
\fi
% I would recommend putting the following in your preamble to format question documents:
% Gives minted code blocks ~80 chars width
\usepackage[a4paper, total={6.25in, 8in}]{geometry}
% Uses Helvetica font
\usepackage{helvet}
\usepackage[T1]{fontenc}
\renewcommand{\familydefault}{\sfdefault}
% Sets minted line numbers to normal font size
\renewcommand{\theFancyVerbLine}{{\normalsize \arabic{FancyVerbLine}}}
% Removes paragraph indent and adds space between paragraphs
\linespread{1.4}
\setlength\parskip{0.5em}
\setlength\parindent{0em}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment