3

Long time listener, first time caller. Please be gentle; I will do my best to abide by the rules, but it will be a bit difficult for me to do. Allow me a small explanation:

My intro to LaTeX started with a template from my first physics instructor 5+ years ago. In the time since then, I've cranked out various beautiful and crisp lab reports/technical documents/resumes/etc with this wonderful language, but unfortunately, I've never really had time to learn the language well so much as I've just reused/tweaked/googled my way to success. So, in the end, although I've tried to be good with commenting new things for future reference, and have produced quite a few lines of code and many-an awesome looking document, I don't REALLY know what I'm doing as far as the more advanced front matter; I usually just cut/paste all front matter from one document to the next, since I'm not sure what odd piece might be the glue holding something important together. Hence, things may be a bit "Frankenstein-y" as far as being patched together in odd/potentially unnecessary ways.

I explain this because I'm unable to provide a truly "minimum" working example, since I don't really know which packages/commands/environments might be necessary/redundant/conflicting. I thus feel compelled to include my entire front matter + the minimum conflict areas, in the hopes that somebody can help me sort things out a bit.

I tried searching the forums before posting this, but can't seem to find a working solution specific to what I've got going on here. Much thanks in advance for any helpful input anyone might be able to provide.

PROBLEM DESCRIPTION: I am trying to \ref{}, within a \pmatrix environment that is itself with an \align{} environment, an Appendix with a \label{} at the end of my document. The Appendix is given using a \newcommand called \question and has a manual bookmark. I've tried putting the \label{} command in various places, before and after the bookmark, for example; I've tried putting the \ref{} command itself in different places within the pmatrix/align environment, including inside a \text{} command in case that was the issue; I've even tried playing with the ordering of the bookmark and hyperref packages in the front matter. But all to no avail. The strange thing is, when I hover over the \ref{} (in Texstudio), it correctly identifies the \label and what it is referring to, and hovering over the \label itself correctly identifies "1 reference(s) to this label". The label also shows up in the auto drop down label list when I type \ref. However, when I hover over the relevant \end{align} tag, the reference is displayed as ??, and then, when I actually output to a .pdf, nothing shows up at all. I've seen the ?? before in an output .pdf and dealt with it, sometimes by nothing more than recompiling a second time, but this conflicting behaviour (and nothing showing up in the .pdf at all) is new to me. (And I'm not sure if this is relevant, but also I switched over from TexMaker only a month or so ago. So everything Texstudio is new to me as well.)

\documentclass{article}

\usepackage{amsmath,amssymb,amsthm}
\usepackage{hyperref}
\usepackage[numbered]{bookmark}

\newcommand\question[2]{\vspace{.25in}\hrule\textbf{#1: #2}\vspace{.5em}\hrule\vspace{.10in}}
\newcommand\headNote[2]{\vspace{.25in}\hrule\textbf{#1 #2}\vspace{.5em}\hrule\vspace{.10in}}
\begin{document}

\begin{align*}
e^{\begin{pmatrix}1 & \lambda\\0 &1\end{pmatrix}} &= \mathbb{1} + M + \frac{M^{2}}{2!} + \frac{M^{3}}{3!} + ... + \frac{M^{n}}{n!}\\
&...\\
&= \begin{pmatrix}\left[1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ...\right] &\left[0 + \frac{\lambda}{1!} + \frac{2\lambda}{2!} + \frac{3\lambda}{3!} + ...\right]^{\text{(\ref{B})}}\\0 & \left[1 + 1 + \frac{1}{2!} + \frac{1}{3!} + ...\right] \end{pmatrix}\\
&...
\end{align*}

\newpage

\question{\large{Appendix B - Esplains}}{}
\bookmark[page = 2, level = 0]{APPENDIX B}
\headNote{Note:  Here we feel compelled to esplains a step in Problem 8... blah blah:}{}\label{B}

math stuff

\end{document}
Swerve
  • 31
  • 2
    I think your fundamental issue is that a \label needs a numbered thing to attach itself to (a chapter, section, equation, etc.), and you've provided none in this document. Taking the liberty of updating your MWE with a much smaller document that still shows the empty () reference. – Mike Renfro Oct 01 '16 at 01:02
  • Ah, okay. Thank you for the info. In that case, is there something besides \label I can use? Iirc, I tried to reference or maybe even \cite the bookmark itself when I first tried and failed to do this a few weeks ago, but I couldn't get that to work either. I'm not sure I know of other similar style commands for cross-referencing stuff. – Swerve Oct 01 '16 at 01:31
  • Welcome! Why aren't you using e.g. \appendix\section{Esplains}? Manually numbering things is exactly the kind of thing humans are useless at and computers are great at. (Not to mention you'll get a whole bunch of automatic stuff done by hyperref this way.) Also, don't use e.g. \large in the argument. Customise that stuff globally. In any case \large is a switch - it doesn't take an argument. – cfr Oct 01 '16 at 01:32
  • Basically, what @cfr just said. In a perfect world, what would your reference point to? A document division like an appendix, an equation or sub-equation, etc.? – Mike Renfro Oct 01 '16 at 01:33
  • You don't need to load hyperref. bookmark will do it anyway. – cfr Oct 01 '16 at 01:34
  • Oh, is \appendix\section an actual thing? I wasn't aware of that. The reason I used the \question environment there is because I really like the way it looks, with the hlines and such, and I wanted that appendix header to be consistent with the previous \question document headers. – Swerve Oct 01 '16 at 01:35
  • Then customise the format. Unless you prefer to define all of the apparatus required to get your custom code to do all the stuff which \section etc. does for you. Actually, I pretty much do that in a custom class I use, but you still want to separate format from content and you absolutely need to do it with counters, make sure you are writing the correct stuff to auxiliary files etc. Using \section would be a whole bunch easier. Just customise it however. (Even using a counter for the questions would help.) – cfr Oct 01 '16 at 01:39
  • 1
    \label{} picks up the last \refstepcounter, basically. If you don't have anything which \refstepcounters, there's nothing to reference. – cfr Oct 01 '16 at 01:40
  • However, if you've been using LaTeX for 5+ years and you don't know that \appendix\section is a real thing, then you will save yourself a great deal of time and frustration if you read a basic introduction. You may think you do not have time, but you will spend far, far longer if you don't and still you probably won't get good results. (You've probably spend many times as much time cutting-pasting-googling as you would have if you'd read a basic introduction 5 years ago. But there's no need to make it 10 or 20 years.) – cfr Oct 01 '16 at 01:42
  • @cfr Oops, thanks for pointing that out; I think that's just leftover from me fiddling/trying to figure things out but forgetting to clean up afterwards. It does nothing there. But tbh "globally" is where I fall apart (re: me not really knowing how to code LaTex). I can generally make things work/look pretty eventually, usually with local on-the-fly commands, but I'm afraid I'm a bit useless with front matter/global stuff. RE: hyperref: Oh, thanks for pointing that out too. That was a late addition in my quest to figure this out, but I did notice that it changed nothing. – Swerve Oct 01 '16 at 01:51
  • Using \section etc. to structure a document is pretty much abc. It is pretty much the point. Really, go read an introduction. I cover this in the first couple of hours of a workshop aimed at complete beginners who first have to learn how to find somewhere to write it. bookmark must be a complete PITA if you don't use any semantic mark-up at all. – cfr Oct 01 '16 at 01:56
  • Also, you have no glue in your definitions so if you're not using any of the usual commands which include such glue, you must be doing without it which will produce less than ideal results. – cfr Oct 01 '16 at 02:03
  • @ cfr Ah, okay, I thought \label was more magical and was just referencing {whatever I tell it to}. As far as a basic LaTex intro... sigh. There's so much other stuff I should/could be learning! Dammit. This is the first time I haven't been able to bluster my way past a problem; Stackexchange is a powerful tool. Plus I google fast. Good advice though, I suppose I'll have to do it sometime. (You lost me @ customizing \section, although I think I did that once by editing a .sty file or something? Or using \renewcommand? I'll look into it.) Thank you for your help (@ MR as well). – Swerve Oct 01 '16 at 02:04
  • Oh, don't get me wrong, I've used \section \subsection etc in the past for reports. But in this case, I downloaded some HW template (not sure from where, I looked through quite a few until I found one I mostly liked), so all the front matter \newcommand stuff you see (except \headMe and \headNote and some new packages) are from that template. I thought there was a good reason that the person defined \question and such and used them in place of \section, I just followed suit and adjusted to my liking. Didn't really consider the numbering stuff behind the scenes until you mentioned it. – Swerve Oct 01 '16 at 02:08
  • If all you want is to hyperlink something, use \hypertarget and \hyperlink. If you want to \label and \ref something other than a number, that can be done too (see http://tex.stackexchange.com/questions/315530/get-label-counter/315612?s=1|0.0000#315612 for example). – John Kormylo Oct 01 '16 at 15:06
  • Related, in terms of making a basic template: Template for Standard Operating Procedures? – Mike Renfro Oct 01 '16 at 16:18

1 Answers1

2

Update with crossreftools at the end of this post!

This answers only the label and reference issue itself from the TeX/LaTeX side, not possible issue with TeX editors.

If there is no counter involved with \label the macro can't really provide any information about the name, value and hyperanchor. This can be fixed with a \phantomlabel{}{} macro that provides a hyperanchor with \phantomsection and \@currentlabel and \@currentlabelname.

\phantomlabel{B}{App B} will insert a label named B, the reference to it will yield App B later on.

\documentclass{article}

\usepackage{amsmath,amssymb,amsthm}
\usepackage{hyperref}
\usepackage[numbered]{bookmark}

\makeatletter
\AtBeginDocument{%
\newcommand{\phantomlabel}[2]{%
  \begingroup
  \phantomsection
  \def\@currentlabel{#2}%
  \def\@currentlabelname{#2}%
  \label{#1}%
  \endgroup
}
}
\makeatother

\newcounter{question}


\newcommand\question[2]{\vspace{.25in}\hrule\textbf{#1: #2}\vspace{.5em}\hrule\vspace{.10in}}
\newcommand\headNote[2]{\vspace{.25in}\hrule\textbf{#1 #2}\vspace{.5em}\hrule\vspace{.10in}}
\begin{document}

\begin{align*}
e^{\begin{pmatrix}1 & \lambda\\0 &1\end{pmatrix}} &= \mathbb{1} + M + \frac{M^{2}}{2!} + \frac{M^{3}}{3!} + ... + \frac{M^{n}}{n!}\\
&...\\
&= \begin{pmatrix}\left[1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ...\right] &\left[0 + \frac{\lambda}{1!} + \frac{2\lambda}{2!} + \frac{3\lambda}{3!} + ...\right]^{\text{(\ref{B})}}\\0 & \left[1 + 1 + \frac{1}{2!} + \frac{1}{3!} + ...\right] \end{pmatrix}\\
&...
\end{align*}


\newpage

\question{\large{Appendix B - Esplains}}{}\phantomlabel{B}{App. B}
\bookmark[page = 2, level = 0]{APPENDIX \ref{B}}

\headNote{Note:  Here we feel compelled to esplains a step in Problem 8... blah blah:}{}

math stuff

\end{document}

enter image description here

Update

The \phantomlabel feature is now part of my crossreftools package, however with a different name and syntax:

\phantomlabel{B}{App. B}\crtcrossreflabel*{App. B}[B].

\documentclass{article}

\usepackage{amsmath,amssymb,amsthm}
\usepackage{hyperref}
\usepackage[numbered]{bookmark}

\usepackage{crossreftools}

\newcounter{question}


\newcommand\question[2]{\vspace{.25in}\hrule\textbf{#1: #2}\vspace{.5em}\hrule\vspace{.10in}}
\newcommand\headNote[2]{\vspace{.25in}\hrule\textbf{#1 #2}\vspace{.5em}\hrule\vspace{.10in}}
\begin{document}

\begin{align*}
e^{\begin{pmatrix}1 & \lambda\\0 &1\end{pmatrix}} &= \mathbb{1} + M + \frac{M^{2}}{2!} + \frac{M^{3}}{3!} + ... + \frac{M^{n}}{n!}\\
&...\\
&= \begin{pmatrix}\left[1 + \frac{1}{1!} + \frac{1}{2!} + \frac{1}{3!} + ...\right] &\left[0 + \frac{\lambda}{1!} + \frac{2\lambda}{2!} + \frac{3\lambda}{3!} + ...\right]^{\text{(\ref{B})}}\\0 & \left[1 + 1 + \frac{1}{2!} + \frac{1}{3!} + ...\right] \end{pmatrix}\\
&...
\end{align*}


\newpage

\question{\large{Appendix B - Esplains}}{}\crtcrossreflabel*{App. B}[B]
\bookmark[page = 2, level = 0]{APPENDIX \ref{B}}

\headNote{Note:  Here we feel compelled to esplains a step in Problem 8... blah blah:}{}

math stuff

\end{document}