General\newenvironment not going to new line

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
guest

\newenvironment not going to new line

Post by guest »

Hi,
I'm trying to setup a custom document for my maths assignments. I've modified one of the internet but it's not giving me the output I'm aiming for.

Code: Select all

\newenvironment{problem}[2][Problem]{\begin{trivlist} 
\item[\hskip \labelsep {\bfseries #1} \hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}

\newenvironment{question}[2][Question]
{
\begin{trivlist}

\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}] 
\\
}

{\end{trivlist}}
Basically I want my document to output the following:
Question 1

Problem (a)

When given this:

Code: Select all

\begin{question}{1}
\begin{problem}{(a)}
but I cannot get it to do so! even when using this:

Code: Select all

\begin{question}{1}
\\
\begin{problem}{(a)}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

\newenvironment not going to new line

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

Just a small advice: even if your code snippets may be theoretically sufficient to describe the problem, the chance to get a solution would be higher if you would post a Infominimal working example, i.e. some compilable code which could be tested and improved. Experience shows, that compilable examples get answers and fixed most of the time, because it's easy to try, whereas often snippets remain untested and unfixed. Making it easy for answerers is a good idea.

Stefan
LaTeX.org admin
guest

\newenvironment not going to new line

Post by guest »

Okay thanks !

Code: Select all

\documentclass[12pt]{article}
 
\usepackage[margin=1in]{geometry} 
\usepackage{amsmath,amsthm,amssymb}
 
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
 
\newenvironment{problem}[2][Problem]{\begin{trivlist} 
\item[\hskip \labelsep {\bfseries #1} \hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}

\newenvironment{question}[2][Question]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}\hskip \labelsep {\bfseries #2.}]}{\end{trivlist}}


\begin{document}
 
\begin{question}{1} %You can use theorem, exercise, problem, or question here.  Modify x.yz to be whatever number you are proving
\begin{problem}{(a)}
\end{problem}

\end{document}
Compiling this you'll see that Question 1. and Problem(a) are on the same line where I'd like an automatic new line after Question 1. is written.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

\newenvironment not going to new line

Post by cgnieder »

Adding \mbox{}\\ at the appropriate place should work. Infominimal working example ensures that there is something typeset (even if it has zero width) so that a line can be ended.

However, you can have the whole thing with a more convenient interface:

Code: Select all

\documentclass{article}
\usepackage{amsthm}

\newtheoremstyle{question}
  {\topsep} % Space above
  {\topsep} % Space below
  {} % Body font
  {} % Indent amount
  {\bfseries} % Theorem head font
  {.\newline} % Punctuation after theorem head
  {0em} % Space after theorem head
  {} % Theorem head spec (can be left empty, meaning `normal')
\theoremstyle{question}
\newtheorem{question}{Question}

\newtheoremstyle{problem}
  {\topsep} % Space above
  {\topsep} % Space below
  {} % Body font
  {} % Indent amount
  {\bfseries} % Theorem head font
  {} % Punctuation after theorem head
  {.5em} % Space after theorem head
  {} % Theorem head spec (can be left empty, meaning `normal')
\theoremstyle{problem}
\newtheorem{problem}{Problem}
\renewcommand\theproblem{(\alph{problem})}

\begin{document}

\begin{question}
 This is a question
 \begin{problem}
  This is the first problem
 \end{problem}
\end{question}

\end{document}
For details please refer to the amsthm documentation.

Regards
site moderator & package author
guest

Re: \newenvironment not going to new line

Post by guest »

Cool! Thanks.
Post Reply