GeneralUser-defined environment issue

LaTeX specific issues not fitting into one of the other forums of this category.
20-LESI
Posts: 9
Joined: Thu Feb 23, 2012 3:02 am

User-defined environment issue

Post by 20-LESI »

Hi there!

I defined an environment "Protocolo" like this:

Code: Select all

\newenvironment{Protocolo}{      % Protocolo environment
\refstepcounter{protcounter}
\begin{framed}
  \begin{center} {       % Centering minipage
    \begin{minipage}[b]{380px}    % Starts minipage
     \par\medskip\noindent%
       \textbf{Protocolo \theprotcounter}    % Set's title and starts italic for text 
    \end{minipage}}         % End minipage
}{\end{center}\end{framed}}            % End Protocolo environment
I wish to change this code, in order to prevent the boxes from the framed environment from being split across different pages. Is there a way to do this?

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

User-defined environment issue

Post by cgnieder »

A boxedminipage should do:

Code: Select all

\documentclass{article}
\usepackage{boxedminipage}
\usepackage{lipsum}% produces dummy-text

\makeatletter
\newcounter{protcounter}
\newenvironment{Protocolo}{%
  \refstepcounter{protcounter}%
  \noindent
  \begin{boxedminipage}{\linewidth}
    \medskip\noindent\textbf{Protocolo \theprotcounter}%
    \itshape
    \begin{center}}
{%
  \end{center}
  \end{boxedminipage}%
  \par\addvspace{\baselineskip}%
  \@endpetrue}
\makeatother

\begin{document}

\begin{Protocolo}
 \lipsum[1-2]
\end{Protocolo}

\lipsum[3]

\begin{Protocolo}
 \lipsum[4-5]
\end{Protocolo}

\end{document}
Regards
site moderator & package author
20-LESI
Posts: 9
Joined: Thu Feb 23, 2012 3:02 am

User-defined environment issue

Post by 20-LESI »

Hi! First of all, thanks for the reply!

That solved the problem, but I was looking for a different solution. Imagine a latex file with three components, in the presented order:

A = chunk of text
P = some text in Protocolo environment
B = chunk of text

If A+P doesn't fit in a single page, I wanted latex to "grab" the necessary text from B to completely fill a page. With your solution, latex does the following:

PAGE 1
A
(a lot of free space here)

PAGE 2
P
B
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

User-defined environment issue

Post by cgnieder »

This sounds like you would want a floating environment. You could define it with the floatrow package:

Code: Select all

\documentclass{article}
\usepackage{floatrow,caption}
\usepackage{lipsum}% produces dummy-text

\DeclareNewFloatType{Protocolo}{placement=ht}
\DeclareFloatVCode{protocolo}{\refstepcounter{Protocolo}\textbf{Protocolo \theProtocolo}\hfill\newline}
\floatsetup[Protocolo]{style=BOXED,capposition=TOP,font=it,precode=protocolo}
\captionsetup[Protocolo]{labelfont=bf,justification=raggedright}
% Do _not_ use \caption{} with the `Protocolo' environment!

\begin{document}

\begin{Protocolo}
 \lipsum[1-2]
\end{Protocolo}

\lipsum[3]

\begin{Protocolo}
 \lipsum[4-5]
\end{Protocolo}

\lipsum[6-9]

\end{document}
Regards
site moderator & package author
20-LESI
Posts: 9
Joined: Thu Feb 23, 2012 3:02 am

User-defined environment issue

Post by 20-LESI »

cgnieder wrote:This sounds like you would want a floating environment.
Once again thanks for your help! That's exactly what I wanted, but I didn't know the name. That solved the problem, but I now have two different issues:

:arrow: The references I had for all my "Protocolos" now appear with "??", but they actually work. How can I make it print the Protocolo number inside it? I couldn't done it myself looking into floatrow documentation...

:arrow: In one of the document's pages it got messed up, because the right column somehow had its margins changed. Here's a print:

Image

The Protocolo in this page came from another subsection. If I force a new page (\clearpage) before the \begin{Protocolo}, the problem vanishes. That's a naive solution, because I may have to do this very often and it leaves blank space in the previous page... Any tip on how to solve this with a better solution?


My best regards,

Celso
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

User-defined environment issue

Post by cgnieder »

The first issue is most probably because \caption isn't used. That's easily fixed.

Code: Select all

\documentclass{article}
\usepackage{floatrow,caption}
\usepackage{lipsum}% produces dummy-text

% new float type:
\DeclareNewFloatType{protocolo}{placement=ht,name=Protocolo}
\floatsetup[protocolo]{style=BOXED,capposition=TOP,font=it,floatwidth=\linewidth}
\captionsetup[protocolo]{labelfont=bf,justification=raggedright,singlelinecheck=off}

% wrapper so we don't need to call \caption
\newenvironment{Protocolo}{\begin{protocolo}\caption{}}{\end{protocolo}}
\begin{document}

\begin{Protocolo}\label{prt:testa}
 \lipsum[1-2]
\end{Protocolo}

See protocolo \ref{prt:testb}

\lipsum[3]

\begin{Protocolo}\label{prt:testb}
 \lipsum[4-5]
\end{Protocolo}

\lipsum[6-9]

\end{document}
As for the second issue: it is nearly impossible to tell what's happening exactly without a Infominimal working example. Does the floatwidth=\linewidth help? If it doesn't please provide an example code which shows the problem.

Regards
site moderator & package author
20-LESI
Posts: 9
Joined: Thu Feb 23, 2012 3:02 am

User-defined environment issue

Post by 20-LESI »

cgnieder wrote:The first issue is most probably because \caption isn't used. That's easily fixed.
That worked, but a minor issue appeared: the label font size decreased. I don't understand how that happens, and I was unable to change it's size. Any idea?

cgnieder wrote: As for the second issue: it is nearly impossible to tell what's happening exactly without a Infominimal working example. Does the floatwidth=\linewidth help? If it doesn't please provide an example code which shows the problem.
That command didn't help. I tried to make a minimal example, but I was unable to reproduce the error. It seems that it just happens with my document :evil:


Here's the code around the error, I doubt this helps:

Code: Select all

A implementação dum esquema de assinaturas tendo por base a ideia descrita foi também avançada por Shamir no mesmo artigo. O protocolo \ref{prot:shamir_iba} contém essa implementação, 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Melhorar o anexo respetivo, colocando lá informação acerca de como quebrar o protocolo, em que é que reside a sua segurança, etc. Ler o paper do Shamir de 1984 para tal%%%%%%% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Desde a publicação do artigo de Shamir até à atualidade vários esquemas IBA foram propostos. Uma implementação muito conhecida deste tipo de assinaturas pode ser encontrada em \cite{Cha02anidentity-based}. Este esquema de assinaturas digitais pode ser construído sobre qualquer grupo GDH. A sua implementação encontra-se no protocolo \ref{prot:sig_iba_cha_cheon}, sendo mais uma vez considerado um exemplo onde Alice pretende enviar uma mensagem e respetiva assinatura para Bob.

\begin{Protocolo}\label{prot:sig_iba_cha_cheon}

\begin{itemize}

\item \uline{Construção das chaves do KGC}
	\begin{enumerate}
	\item Escolher um gerador $P$ de um grupo GDH $G$ de ordem $l$.
	\item Escolher a chave mestra privada $s \in \mathbb{Z}_l$.
	\item Computar a chave mestra pública $P_{pub} = sP$.
	\item Escolher duas funções de \emph{hash} criptográficas $H_1 : \{0, 1\}^{*} \times G \rightarrow \mathbb{Z}_l$ e $H_2 : \{0, 1\}^{*} \rightarrow G$.
	\end{enumerate}

\item \uline{Construção das chaves de Alice}
	\begin{enumerate}
	\item A chave pública de Alice consiste na aplicação de $H_2$ sobre um qualquer item identificador, por exemplo o número do BI (Bilhete de Identidade) de Alice. Vamos denotar este item como ID.
	
		\begin{center}
		$Q_{ID} = H_2(ID)$
		\end{center}
	
	\item A chave privada é computada no KGC da seguinte forma:
	
		\begin{center}
		$D_{ID} = sH_2(ID)$
		\end{center}
	
	\end{enumerate}

\item \uline{Assinar uma mensagem $m$} - Alice executa os passos que se seguem.
	\begin{enumerate}
	\item Escolher um número aleatório $r \in \mathbb{Z}_l$.
	\item Computar $U = rQ_{ID}$.
	\item Calcular $h = H_1(m, U)$.
	\item Determinar $V = (r + h)D_{ID}$.
	\item A assinatura de Alice sobre a mensagem é (U, V).
	\end{enumerate}

\item \uline{Verificar a assinatura de $m$}
	\begin{enumerate}
	\item Calcular $h = H_1(m, U)$.
	\item Para verificar a assinatura, Bob tem de verificar se o tuplo $(P, P_{pub}, U + hQ_{ID}, V)$ é um tuplo DH válido, através da decisão:
	
		\begin{center}
		$[\hat{e}(P,V) = \hat{e}(sP, U + hQ_{ID})] ? $
		\end{center}
	
	\end{enumerate}

\end{itemize}

\end{Protocolo}

%%%%%%%%%%%%%%%%%%%%%%
% Complementar o exemplo mencionado %
%%%%%%%%%%%%%%%%%%%%%%

\subsection*{Comparação entre assinaturas PKI e IBA} \label{vs}
Os sistemas criptográficos baseados em técnicas PKI e IBA são ambos assimétricos. Logo, os protocolos de assinatura e verificação de assinaturas possuem funcionalidades semelhantes nos dois sistemas. A grande diferença reside na gestão das chaves.
If I insert a clearpage before the \begin{Protocol}, forcing the Protocol to begin in another page, as with the following subsection, the problem is solved, but it leaves the blank text in the previous page. My attemps on making the minimal example failed, because it solved the same problem in various locations...
Last edited by 20-LESI on Sun Feb 26, 2012 5:54 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

User-defined environment issue

Post by cgnieder »

20-LESI wrote:That worked, but a minor issue appeared: the label font size decreased. I don't understand how that happens, and I was unable to change it's size. Any idea?
The appearance of the counter can be redefined by redefining \the<counter>.

Code: Select all

\renewcommand*\theprotocolo{\small\arabic{protocolo}}
20-LESI wrote:My attemps on making the minimal example failed, because it solved the same problem in various locations
Then you're on the best way. There are two main issues why MWEs are so important:
  1. It allows helpers to see possible reasons for errors which may depend on the documentclass and very often on the packages used
  2. It enables oneself to find the parts which cause the error or unwanted behaviour.
The first point is why I can't help with the code you provided[*].

But it seems you're on the best way yourself. If you're minimal examples doesn't show the issues then they're caused by the code you're not using, obviously.

Regards

[*] There is a code marker for that, by the way. Using that instead of the quote marker would make your post more readable.
site moderator & package author
20-LESI
Posts: 9
Joined: Thu Feb 23, 2012 3:02 am

User-defined environment issue

Post by 20-LESI »

cgnieder wrote:
20-LESI wrote:That worked, but a minor issue appeared: the label font size decreased. I don't understand how that happens, and I was unable to change it's size. Any idea?
The appearance of the counter can be redefined by redefining \the<counter>.

Code: Select all

\renewcommand*\theprotocolo{\small\arabic{protocolo}}
With that code, I can only change the size number. Like this:

Protocolo 1
Protocolo 1
Protocolo 1

I also want to be able to change "Protocolo" size.

cgnieder wrote:
20-LESI wrote:My attemps on making the minimal example failed, because it solved the same problem in various locations
Then you're on the best way. There are two main issues why MWEs are so important:
  1. It allows helpers to see possible reasons for errors which may depend on the documentclass and very often on the packages used
  2. It enables oneself to find the parts which cause the error or unwanted behaviour.
The first point is why I can't help with the code you provided[*].

But it seems you're on the best way yourself. If you're minimal examples doesn't show the issues then they're caused by the code you're not using, obviously.
I will try to solve the problem then! Thanks!

cgnieder wrote: [*] There is a code marker for that, by the way. Using that instead of the quote marker would make your post more readable.
Sorry, I got the wrong marker and didn't notice!
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

User-defined environment issue

Post by cgnieder »

20-LESI wrote:With that code, I can only change the size number. Like this:

Protocolo 1
Protocolo 1
Protocolo 1

I also want to be able to change "Protocolo" size.
The Protocolo <num> part is the caption of the float. This means it can be customized with \captionsetup

Just add something like
  • font=small or
  • labelfont=small
to the caption setup.

You'll find more on this in the manual of the caption package.

Regards
site moderator & package author
Post Reply