7

I use a new environment and have tried to refer to it using the \label-\refcommands. I'm running into the problem that the \label command affects the vertical position of the caption.

The MWE is:

\documentclass[a4paper]{memoir}
\usepackage{lipsum}

%%% caption for new environment
\makeatletter
\newcommand{\mycaption}[1]{%
 \strictpagecheck\marginpar{\setlength{\parindent}{1.0em}\small
  \@afterindentfalse\@afterheading {\textbf{Caption \themynewcount:}} #1}%
}
\makeatother

%%% new environment
\newcounter{mynewcount}[chapter]
\renewcommand{\themynewcount}{\thechapter.\arabic{mynewcount}}   

\newenvironment{mynewenv}[1]{%
 \refstepcounter{mynewcount}%
 \vspace{2\abovedisplayskip}%
 \mycaption{#1}%
 \vspace{-\abovedisplayskip} 
 \noindent\begin{minipage}[t]{\textwidth}%
 }{\end{minipage}\vspace{\belowdisplayskip}}

\begin{document}
\chapter{Chapter}
\lipsum[1]
\begin{mynewenv}{Caption}
\label{test} % <<< label
\lipsum[2]
\end{mynewenv}
\ref{test} % <<< refer to label
\end{document}

Without the \label and \ref commands, the caption is properly aligned with the top of the contents of the environment:

enter image description here

With them, however, the caption is shifted up:

enter image description here

How can I fix this?

4

Warning: since your code uses marginpar it could not be well aligned with the mini page.

You could change the mynewenv environment definition to have an optional argument to be used within \label, as shown below:

\newenvironment{mynewenv}[2][]{% <-- here
 \refstepcounter{mynewcount}%
 \vspace{2\abovedisplayskip}%
 \mycaption{#2}\label{#1}%       <-- here
 \vspace{-\abovedisplayskip} 
 \noindent\begin{minipage}[t]{\textwidth}%
 }{\end{minipage}\vspace{\belowdisplayskip}}

Then, you simply pass the label as option, like:

\begin{mynewenv}[test]{Caption}
\lipsum[2]
\end{mynewenv}
As we can see in \ref{test}...

Or without the option (but in this case, some warning could happens if using multiple times):

\begin{mynewenv}{Caption}
\lipsum[2]
\end{mynewenv}

enter image description here

| improve this answer | |
  • If the optional argument is not used, \label{} will occur, which is no problem, but awful, but you will get multiply defined labels warnings when using the environment twice or more times without setting the optional argument. – user31729 Dec 28 '18 at 16:31
  • @ChristianHupfer, ow, that is true. Some use of nextchar could help. – Sigur Dec 28 '18 at 16:34
  • Easier: Use xparse and \NewDocumentEnvironment{mynewenv}{o+m}{... \IfValueT{#1}{\label{#1}} ... rest as given, for example)}{\end{minipage}} – user31729 Dec 28 '18 at 16:36
  • Actually, the optional argument is the better way, since the label should occur after the \mycaption and not in the environment body, in my point of view – user31729 Dec 28 '18 at 16:44
  • @ChristianHupfer I am having problem to align to mini page because the depth, I think. – Sigur Dec 28 '18 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.