chrmatt / algpseudocodex

LaTeX package for typesetting pseudocode.
38 stars 2 forks source link

Labels don't work #1

Closed chiumichael closed 3 years ago

chiumichael commented 3 years ago

\labels all show up as "1".

I'm not well versed in latex but could it be similar to this issue?

Love the package otherwise!

chrmatt commented 3 years ago

Can you provide a minimal example where this happens? The following code works fine for me:

\documentclass{article}
\usepackage{algpseudocodex}
\begin{document}
    \begin{algorithmic}[1]
        \State First line \label{s1}
        \State Second line \label{s2}
    \end{algorithmic}
    Reference to line~\ref{s1} and line~\ref{s2}.
\end{document}
chiumichael commented 3 years ago

Hi Chrsitian,

Thanks for the prompt reply. I did a quick test and I think it has something to do with subfiles. Here's a minimal example of what I have:

main.tex

\documentclass{article}
\usepackage{subfiles}           % for separate compilation 
\providecommand{\main}{.}
\usepackage{algorithm}
\usepackage{algpseudocodex}

\begin{document}

Line should be one:  \ref{subf-line-1}
Line should be two: \ref{subf-line-2}

\begin{algorithm}
\begin{algorithmic}[1]
    \State First line \label{s1}
    \State Second line \label{s2}
\end{algorithmic}
\end{algorithm}
Reference to line~\ref{s1} and line~\ref{s2}.

\subfile{alg}

\end{document}

alg.tex

\begin{algorithm}
\begin{algorithmic}[1]
\caption{My algorithm}
\label{alg:my_alg}

\State please work \label{subf-line-1}
\State please also work \label{subf-line-2}

\end{algorithmic}
\end{algorithm}

I have the above in a fresh document on overleaf reproducing the behavior that I see (all references come up as "1")

chrmatt commented 3 years ago

The problem with your code is that you use \caption inside the algorithmic environment. It should be inside algorithm, but outside algorithmic. That is, the file alg.tex should look like this instead:

\begin{algorithm}
\caption{My algorithm}
\label{alg:my_alg}
\begin{algorithmic}[1]
\State please work \label{subf-line-1}
\State please also work \label{subf-line-2}
\end{algorithmic}
\end{algorithm}

That gives me correct references for both algorithms.

chiumichael commented 3 years ago

Ahh, everything works like a charm now.

Thanks a lot for taking the time, I appreciate it.