TransWikia.com

How can I keep the content of one environment with the next paragraph from another environment on the same page?

TeX - LaTeX Asked on May 2, 2021

I am working with environments to be able to easily format the content of a document.

Now I want to define a rule so that the content from the ccetheme environment appears on the same page as the content from the next environment: it can be ccebody or ccesection

I have seen that it can be done with needspace or minipage, but I can’t do it.

This is my minimal example:

documentclass[ebook,11pt,twoside,openright,showtrims]{memoir}
setstocksize{9in}{6in}
settrimmedsize{9in}{6in}{*}
setbinding{0.5in}
setlrmarginsandblock{0.5in}{0.4in}{*}
setulmarginsandblock{0.5in}{0.5in}{*}
setheadfoot{12.07pt}{footskip}
checkandfixthelayout
usepackage{fontspec}
newfontfamilyheaderfont{Libertinus Sans}
usepackage{polyglossia}
setdefaultlanguage{spanish}
usepackage{csquotes} 
usepackage{xcolor}  
usepackage{layout}
usepackage{needspace}  
usepackage{lipsum} 
usepackage[showframe, pass]{geometry}
newcommand{txtred}{textcolor{red}}
newcommandn[1]{txtred{textbf{#1}}}

%--------------------------------------------------------------------- CCE styles
newenvironment{ccetheme}[1]{%
    % begin{cr}{#1}
    nopagebreak
    par% Start a new paragraph
    addvspace{baselineskip}% Space above
    centering% Alignment
    bfseries% Weight
    color{red}% Colour
    {large #1par}% Change to LARGE or Huge
    ignorespaces
}
{% end{cr}
    par% Start a new paragraph
    addvspace{baselineskip}% Space below
    %begin{minipage}[t]{0.5textwidth}
    
    %end{minipage}  
}

%Títulos en negrita del propio CCE
newenvironment{ccesection}{%
    % begin{cr}{#1}
    par% Start a new paragraph
    addvspace{baselineskip}% Space above
    bfseries% Weight
    ignorespaces
}
{% end{cr}
    par% Start a new paragraph
    addvspace{baselineskip}% Space below
}

newenvironment{ccebody}{%
    begin{minipage}{0.5linewidth} 
        %textit{redtext}
        %fontfamily{gentium}selectfont            
    end{minipage}  
}%
{% end{cr}
    %nopagebreak
    %par% Start a new paragraph
    %addvspace{baselineskip}% Space below
}

newenvironment{ccecite}{%
    % begin{cr}{#1}
    par% Start a new paragraph
    addvspace{baselineskip}% Space above
    alterfont% Font
    ignorespaces
    leftskip1cmrelax
    rightskip1cmrelax
}
{% end{cr}
    par% Start a new paragraph
    addvspace{baselineskip}% Space below
}

begin{document}
    section{Temas}
    begin{ccetheme}{La Alianza con Noé} {CEC 56-58, 71}end{ccetheme}
    
    begin{ccebody}
        n{56} lipsum[1-3] 
    end{ccebody}   

    begin{ccetheme}{Test} {CEC NNNN}end{ccetheme}

    begin{ccebody}
        n{57} lipsum[1-3] 
    end{ccebody}   

    begin{ccebody}
        n{58} lipsum[5] 
    end{ccebody}

    begin{ccebody}
        n{59} lipsum[6-8] 
    end{ccebody}

    begin{ccetheme}{Test} {CEC MMMM}end{ccetheme}

    begin{ccebody}
        begin{ccesection}This is title on boldend{ccesection}
        n{59} lipsum[1-3] 
    end{ccebody}   
    
    begin{ccebody}
        n{60} lipsum[5] 
    end{ccebody}
    
end{document}  

As you can see in the images below, in this particular case there is an unpleasant cut between the ccetheme environment and the ccebody environment.

enter image description here

Also in this case, where I use the ccesection environment I would like the content of ccetheme along with the following to go to the next page:

enter image description here

I want to know if there is any way to force ccetheme content to always appear on the same page as the next paragraph.

As you can see, to achieve this I am putting nopagebreak in ccetheme environment and trying to implement minipage in ccebody, but it doesn’t work.

One Answer

This still has a few warnings and spacing could be tweaked but something like this I think is closer to what you want

also use commands when there is fixed number of braced arguments and environments when you need to surround larger chunks of text.

enter image description here

documentclass[ebook,11pt,twoside,openright,showtrims]{memoir}
setstocksize{9in}{6in}
settrimmedsize{9in}{6in}{*}
setbinding{0.5in}
setlrmarginsandblock{0.5in}{0.4in}{*}
setulmarginsandblock{0.5in}{0.5in}{*}
setheadfoot{12.07pt}{footskip}
checkandfixthelayout
usepackage{fontspec}
newfontfamilyheaderfont{Libertinus Sans}
usepackage{polyglossia}
setdefaultlanguage{spanish}
usepackage{csquotes} 
usepackage{xcolor}  
usepackage{layout}
usepackage{needspace}  
usepackage{lipsum} 
usepackage[showframe, pass]{geometry}
newcommand{txtred}{textcolor{red}}
newcommandn[1]{txtred{textbf{#1}}}

%--------------------------------------------------------------------- CCE styles
newcommand{ccetheme}[2]{%
defcceA{#1}%
defcceB{#2}%
}
%Títulos en negrita del propio CCE
newcommand{ccesection}[1]{%
    par% Start a new paragraph
    addvspace{baselineskip}% Space above
    noindenttextbf{#1}%
    par
}


newenvironment{ccebody}{%
    par% Start a new paragraph
    noindentbegin{minipage}[t]{linewidth}%
    setlengthparindent{15pt}%
    ifxcceArelaxelse
    begin{center}
    bfseries% Weight
    color{red}% Colour
    large cceAcceB
    end{center}%
    fi
    globalletcceArelax
    par
}%
{end{minipage}%
par}

newenvironment{ccecite}{%
    % begin{cr}{#1}
    quote
    alterfont% Font
}
{endquote
}
raggedbottom
begin{document}
    section{Temas}
    ccetheme{La Alianza con Noé} {CEC 56-58, 71}
    
    begin{ccebody}
        n{56} lipsum[1-3] 
    end{ccebody}   

    ccetheme{Test} {CEC NNNN}

    begin{ccebody}
        n{57} lipsum[1-3] 
    end{ccebody}   

    begin{ccebody}
        n{58} lipsum[5] 
    end{ccebody}

    begin{ccebody}
        n{59} lipsum[6-8] 
    end{ccebody}

    ccetheme{Test} {CEC MMMM}

    begin{ccebody}
        ccesection{This is title on bold}
        n{59} lipsum[1-3] 
    end{ccebody}   
    
    begin{ccebody}
        n{60} lipsum[5] 
    end{ccebody}
    
end{document}  

Correct answer by David Carlisle on May 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP