1

I want to write math questions and answers in the form of a book, which will be read by students. Can someone help me to create question, solution and tips environment, so that I could add the equations every time? Also I don't understand why my equations are cluttered.

I have attached an image about how the solution should look

enter image description here

\documentclass[12pt]{article}

%i have used few packages

\usepackage[margin= 1cm]{geometry} \usepackage{amsmath} \usepackage{tcolorbox} \usepackage{multicol} \usepackage{xcolor}

%document begins here

\begin{document}

%section heading \section{Questions on Basic integration}

%question with bold and box \begin{tcolorbox} subsection begins here, the word question should be in bold \subsection*{Question 1} Solve the following integral. $$\int \sec x(\sec x+\tan x) d x $$ \end{tcolorbox}

%solution word in bold \subsubsection*{Solution} this is a quick tip, to be highlighted \textcolor{blue}{$$ \begin{array}{l} \int \sec ^{2} x d x=\tan x \ \int \sec x \tan x=\sec x \end{array} $$}

%the solution
$$ \begin{aligned} \int \sec x(&\sec x+\tan x) d x \ &=\int\left(\sec ^{2} x+\sec x \tan x\right) d x \ &=\int \sec ^{2} x d x+\int \sec x \tan x d x \ &=\tan x+\sec x+C \end{aligned} $$

%second question \begin{tcolorbox}

%question 2 begins here \subsection*{Question 2} Solve the following integral. $$\int \frac{1}{x+x\log x} dx$$ \end{tcolorbox}

%solution in bold \subsubsection*{Solution} \textcolor{blue}{$$ \begin{array}{l} \text { Put } 1+x \log x=t \ \text { then, } \frac{1}{x}; d x=d t \end{array} $$} % solution begins \begin{center}$\displaystyle{ \begin{array}{l} \int \frac{1}{x+x \log x} d x \ \text { Simce, } \frac{1}{x} d x=d t \text { and }(1+\log x)=t \ =\int \frac{d t}{t} \ =\log (t)+C \ =\int \log (1+\log x)+C \end{array} }$ \end{center} \end{document}

sRavioli
  • 136
Vinay
  • 19

2 Answers2

3

Something like this? Observe that while I provide code for a macro called \tip, I don't define separate question and answer environments, as I think that doing so wouldn't help with the clutter issue. (Actually, these environments might worsen the clutter issue.) I can only recommend that you become more familiar with the multi-line math environments of the amsmath package.

enter image description here

\documentclass[12pt]{article}
\usepackage[margin=2.5cm,a4paper]{geometry}
\usepackage{amsmath,tcolorbox}

\newcommand\tip[1]{\smash{\boxed{\color{blue}\array{r} #1 \endarray}}} \begin{document}

\section{Questions on basic integration}

\begin{tcolorbox} \subsection*{Question 1} Solve the following integral. \quad$\displaystyle \int \sec x(\sec x+\tan x), dx $ \end{tcolorbox}

\paragraph{Solution}
\begin{align} &\int \sec x(\sec x+\tan x), dx \hspace{3cm} \tip{ \int \sec ^{2} x, dx=\tan x \[1ex] \int \sec x \tan x ,dx=\sec x} \ &\quad=\int\bigl(\sec ^{2} x+\sec x \tan x\bigr), dx \ &\quad=\int \sec ^{2} x, dx+\int \sec x \tan x, dx \ &\quad=\tan x+\sec x+C \end{align}

\begin{tcolorbox} \subsection*{Question 2} Solve the following integral. \quad$\displaystyle\int \frac{1}{x+x\log x},dx$ \end{tcolorbox}

\paragraph{Solution} \begin{align} &\int \frac{1}{x+x \log x}, dx \ &\quad= \int \frac{1}{x(1+\log x)}, dx \hspace{3cm}\tip{\text{\textbf{Tip:} Put } 1+\log x = t \[1ex] \text{then } \frac{1}{x}, dx = dt} \ &\quad=\int \frac{dt}{t} \ &\quad=\log (t)+C \ &\quad=\int \log (1+\log x)+C \end{align}

\end{document}


Addendum to address one of the OP's follow-up questions. One could define a \question macro as follows (in the preamble):

\newcommand\question[2]{%
   \tcolorbox 
   \subsection*{Question #1} 
   Solve the following integral.\quad$\displaystyle #2$ 
   \endtcolorbox}

and then write \question{1}{\int \sec x(\sec x+\tan x)\, dx} in the body of the document.

Mico
  • 506,678
  • 2
    +1 for this beautiful and simple formatting! – Jes Mar 16 '21 at 17:16
  • @Jes - Many thanks for your compliment! – Mico Mar 16 '21 at 17:19
  • and when i use multi columns, the alignment of tip is changing. can i insert tip after solution ? – Vinay Mar 16 '21 at 17:51
  • @Vinay - You can insert \tip anywhere you want. The only condition is that \tip has to occur in math mode, – Mico Mar 16 '21 at 17:52
  • @Vinay - Have you tried changing \subsection*{...} to \subsection{...}? – Mico Mar 16 '21 at 17:53
  • @Vinay - Why would you want to replace the \tip macro with a tip environment? – Mico Mar 16 '21 at 17:55
  • @Mico sorry its not tip. its question and solution. can we create a macro for question and answer ? – Vinay Mar 16 '21 at 18:03
  • when i am trying to align in two columns, \tip is not changing its position. can we insert tip right beneath the solution ? – Vinay Mar 16 '21 at 18:06
  • @Vinay - In my code, the solutions consists of (a) a header (\paragraph{Solution}) and (b) a multiline equation; this equation will necessarily be different for each and every solution. If you defined a solution environment, it would look like begin{solution} [[write out equation here ]] \end{solution} instead of \paragraph{Solution} [[write out equation here]]. More work, no discernible benefit. – Mico Mar 16 '21 at 18:09
  • @Vinay - Please see the addendum I just posted on how to create a macro (not environment) called \question. – Mico Mar 16 '21 at 18:20
  • @Vinay - On the placement of \tip directives: Please see my earlier comment. – Mico Mar 16 '21 at 18:22
0

Is this what you want to have?

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage[dvipsnames]{xcolor}
\usepackage{mdframed}
   \mdfsetup{
      leftmargin=-5,
      rightmargin=-5,
      innerleftmargin=5,% Distance from content to frame.  To align the content with
      innerrightmargin=5,% the surrouding text, the sum of leftmargin and innerleftmargin must be zero.
      linecolor=white,% The frame consists of inner-, middle-, and outerline.  linecolor changes all three.
      innertopmargin=0\topskip,
   }
   \mdtheorem[backgroundcolor=PineGreen!15!white]{tip}{Tip}%[section] % Uncomment if you want to include section number.
   \mdtheorem{quest}{Question}%[section] % Uncomment if you want to include section number.
   \mdtheorem{sol}{Solution}%[section] % Uncomment if you want to include section number.
\newcommand{\dd}[0]{\,\mathrm{d}} % puts a thin space and an upright 'd'.

\begin{document}

\section{Questions on Basic integration}

\begin{quest} Solve the following integral: \begin{equation} \int \sec x(\sec x+\tan x) \dd x. \end{equation} \end{quest}% % \begin{sol} \begin{tip} \begin{align} \int \sec^{2} x \dd x &= \tan x + C\ \int \sec x \tan x \dd x &= \sec x + C \end{align} \end{tip}% % \begin{align} \int \frac{1}{x+x \log x} \dd x &= \int\frac{1}{x(1+\log(x))} \dd x\ &= \int \frac{\dd t}{t} \ &= \log (t)+C \ &= \log (1+\log x)+C \end{align} \end{sol} \end{document}

enter image description here


EDIT: As you asked in a comment to have no linebreak after 'Question 1', here is an updated version.

\documentclass[12pt]{article}
\usepackage{amsmath,amsthm}
\usepackage[dvipsnames]{xcolor}
\usepackage{mdframed}
\mdfsetup{
   leftmargin=-5,
   rightmargin=-5,
   innerleftmargin=5,% Distance from content to frame.  To align the content with
   innerrightmargin=5,% the surrouding text, the sum of leftmargin and innerleftmargin must be zero.
   linecolor=white,% The frame consists of inner-, middle-, and outerline.  linecolor changes all three.
   innertopmargin=0\topskip,
}

\newtheorem{quest}{Question} % for numbered questions \newtheorem{quest}{Question} % for unnumbered questions \newtheorem{tip}{Tip} % for numbered tips \newtheorem{tip}{Tip} % for unnumbered tips \surroundwithmdframed[backgroundcolor=PineGreen!15!white]{tip} % get the shiny box around (numbered) tips. \surroundwithmdframed[backgroundcolor=PineGreen!15!white]{tip} % get the shiny box around (unnumbered) tips. \newtheorem{sol}{Solution} % for numbered solutions \newtheorem{sol*}{Solution} % for unnumbered solutions

\newcommand{\dd}[0]{,\mathrm{d}} % puts a thin space and an upright 'd'.

\begin{document} \section{Questions on Basic integration} \begin{quest} Solve the following integral: \begin{equation} \int \sec x(\sec x+\tan x) \dd x. \end{equation} \end{quest}% % \begin{sol} ~ % For some reason the tip-environment covers the word 'solution' when there is nothing between them. \begin{tip} Here can be some text. \begin{align} \int \sec^{2} x \dd x &= \tan x + C\ \int \sec x \tan x \dd x &= \sec x + C \end{align} \end{tip}% % \begin{align} \int \frac{1}{x+x \log x} \dd x &= \int\frac{1}{x(1+\log(x))} \dd x\ &= \int \frac{\dd t}{t} \ &= \log (t)+C \ &= \log (1+\log x)+C \end{align} \end{sol} \end{document}

Οὖτις
  • 2,897
  • 1
  • 5
  • 18