1

I have a problem here getting this to work. I have a document where I've manually wrapped the text around a couple figures and math examples (the 2x2 matrix in the attached image). This is done using minipages.

What I want to achieve is the example in the image where the matrix has a vertically centered caption on the side. This caption should be right-adjusted (raggedleft) and the alignment of the math example along with its caption should preferably be centered above the image (like in the example image). I also need a label so I can cross-reference it in the text.

I'm using a macro to generate this layout as it repeats several times throughout the document and due to the math snippets having different sizes it's not ideal to use minipages as I'd prefer to avoid having to manually define the minipage width for each individual instance.

Here's an example:

\documentclass[a4paper, 11pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{blindtext}
\usepackage{mwe}

\makeatletter
\let\mynewlabel\ltx@label
\makeatother
\newcommand{\assignnumber}[1]{%
    \refstepcounter{equation}\mynewlabel{#1}\ref{#1}%
}   % Create caption and label for math

\newcommand{\sidetext}[2]{   % Macro for text minipage
    \begin{minipage}[t]{0.647\textwidth}
        \setlength{\parindent}{0pt}
        \setlength{\parskip}{.5em}
        \vspace{#1}   % Usually 0pt to make top align work
        #2   % Text goes here
    \end{minipage}%
    \hfill
}

\newcommand{\sidemath}[7]{   % Macro for math+figure minipage
    \begin{minipage}[t]{13.3em}
        \vspace{#1}   % Offset to align with text
        #2   % Any extra bits or macros
        \begin{align*}
                &
            #3   % Math portion goes here
            \hspace{-1.5ex}
            \begin{split}
                \textbf{\normalsize Example
                    \assignnumber{mat:#7}:} \\[-\baselineskip\vspace{11pt}] % Label goes here (#7)
                \textit{\normalsize #4}   % Caption goes here (#4)
            \end{split}
                &
        \end{align*}
        \centering
        \vspace{-.7em}
        \includegraphics[width=\linewidth]{#5}
        \captionof{figure}{#6}
        \label{fig:#7}
    \end{minipage}
}

\begin{document}
\section{Example}
\subsection{Matrices}
\sidetext{0pt}{
    \blindtext%
    \textbf{Example \ref{fig:matrix}}, \textbf{Figure \ref{mat:matrix}}
}
\sidemath
{-.7em}
{\Large}
{\left(
    \begin{matrix}
        a & b \\
        c & d \\
    \end{matrix}
    \right)}
{$2\times2$ matrix.}
{example-image-a}
{Lorem ipsum dolor sit amet, consectetuer adipiscing elit.}
{matrix}
\end{document}

This by itself seems to work fine, it compiles and generates the desired output just fine (similar to the example image).

My issue, however, is that as soon as I try to add \usepackage{hyperref} anywhere in the preamble it breaks and gives me a bunch of errors.

After some experimentation I've come to the conclusion that it's most probably the \assignnumber macro that's causing the issue, but I have no idea how to change it to make it be compatible.

Also worth mentioning I'm fairly new to LaTeX ^_^;

tl;dr hyperref causes errors and I'm not sure how to fix them

Expected output

2
  • 3
    Don't show only "the relevant bits" but a small, complete example that demonstrates your problem. May 3 '19 at 21:36
  • Alright, I updated the post with a more complete and self-contained example.
    – Timmy Fox
    May 3 '19 at 22:12
2

Move your definition of \mynewlabel behind the begin of the document, so that it can catch the hyperref definition. And better use \ref* to avoid to get a nonsense link.

\documentclass[a4paper, 11pt]{article}

\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{blindtext}
\usepackage{mwe}


\newcommand{\assignnumber}[1]{%
    \refstepcounter{equation}\mynewlabel{#1}\ref*{#1}%
}   % Create caption and label for math

\newcommand{\sidetext}[2]{   % Macro for text minipage
    \begin{minipage}[t]{0.647\textwidth}
        \setlength{\parindent}{0pt}
        \setlength{\parskip}{.5em}
        \vspace{#1}   % Usually 0pt to make top align work
        #2   % Text goes here
    \end{minipage}%
    \hfill
}

\newcommand{\sidemath}[7]{   % Macro for math+figure minipage
    \begin{minipage}[t]{13.3em}
        \vspace{#1}   % Offset to align with text
        #2   % Any extra bits or macros
        \begin{align*}
                &
            #3   % Math portion goes here
            \hspace{-1.5ex}
            \begin{split}
                \textbf{\normalsize Example
                    \assignnumber{mat:#7}:} \\[-\baselineskip\vspace{11pt}] % Label goes here (#7)
                \textit{\normalsize #4}   % Caption goes here (#4)
            \end{split}
                &
        \end{align*}
        \centering
        \vspace{-.7em}
        \includegraphics[width=\linewidth]{#5}
        \captionof{figure}{#6}
        \label{fig:#7}
    \end{minipage}
}
\usepackage{hyperref}
\AtBeginDocument{\let\mynewlabel\label}
\begin{document}

\section{Example}
\subsection{Matrices}
\sidetext{0pt}{
    \blindtext%
    \textbf{Example \ref{fig:matrix}}, \textbf{Figure \ref{mat:matrix}}
}
\sidemath
{-.7em}
{\Large}
{\left(
    \begin{matrix}
        a & b \\
        c & d \\
    \end{matrix}
    \right)}
{$2\times2$ matrix.}
{example-image-a}
{Lorem ipsum dolor sit amet, consectetuer adipiscing elit.}
{matrix}
\end{document}
1
  • You should check if the numbers are correct if you have more than one of your environments - perhaps you need and additional switch from amsmath. (It is too late for me now) May 3 '19 at 23:16

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.