0

I have a couple of tables in the following runnable, complete, self-contained example, and I can successfully "forward-reference" them by name in the text:

Table [[tab:line-word-byte-count]] tells us that the output
has $1.5~M$ lines, $5.6~M$ words, and $69$ megabytes.

#+NAME: tab:line-word-byte-count
#+CAPTION: Line, word, and byte counts
| 1485702 | 5626026 | 69141951 |

Table [[TestFiles]] contains some filenames we can analyze.

#+NAME: TestFiles
#+CAPTION: List of files names.
| test_file.fbx            |
| armatures_and_things.fbx |

After running org-latex-export-to-pdf, I see the following

enter image description here

Ok, the uderscores in the filenames are causing LaTeX to typeset the names with subscripts, not what I want. So I add ONE line:

Table [[tab:line-word-byte-count]] tells us that the output
has $1.5~M$ lines, $5.6~M$ words, and $69$ megabytes.

#+NAME: tab:line-word-byte-count
#+CAPTION: Line, word, and byte counts
| 1485702 | 5626026 | 69141951 |

Table [[TestFiles]] contains some filenames we can analyze.

#+ATTR_LATEX: :mode verbatim
#+NAME: TestFiles
#+CAPTION: List of files names.
| test_file.fbx            |
| armatures_and_things.fbx |

and the caption AND the reference disappear, but the contents get typeset correctly. It doesn't matter where I put the ATTR_LATEX, before or after the NAME or CAPTION. Any of the six permutations of those three lines gives me the same bad results.

enter image description here

It seems that I can have EITHER captions and references OR verbatim contents, but not both. I'd be grateful for any advice.

EDIT:

I should say that I'd rather not change the contents of the table because I want to feed them to a block of C code, where they are interpreted as strings. Using the proposed answer below, I could chop off the leading and trailing = signs, but then my C code will know about the special formatting I did to get the captions and references. I'd rather not make the C code too aware of the fact that it's being called in the code-block. For example:

#+name:   just-open-and-close
#+header: :exports  both
#+header: :flags    -std=c++14 -lfbxsdk
#+header: :includes '(<iostream> <stdio.h>)
#+header: :var      filenames   = TestFiles
#+BEGIN_SRC C++
  int main (int argc, char ** argv) {
      printf ("filePath: %s\n", filenames[0][0]);
      // ... some more code to open the file ...
  }
#+END_SRC

#+RESULTS: just-open-and-close
: filePath: =test_filefbx=
: An error occurred while loading the file
Reb.Cabin
  • 711
  • 6
  • 19

2 Answers2

1

Looking at the .tex file you produce, setting :mode verbatim prevents org from wrapping the result in \begin{table} and \end{table} in the export. You can achieve the desired result by omitting the #+attr_latex line and using Org markup inside the table:

#+NAME: TestFiles
#+CAPTION: List of files names.
| =test_file.fbx=            |
| =armatures_and_things.fbx= |

Edit: in response to some more clarification.

You can disable the automatic formatting of the underscores with #+options: ^:nil. This may be enough to display and reference the table, but you can also get the monospace font by defining a custom column (you also need to load the array package in latex). Full example:

#+options: ^:nil
#+latex_header: \usepackage{array}
Table [[tab:line-word-byte-count]] tells us that the output
has $1.5~M$ lines, $5.6~M$ words, and $69$ megabytes.

#+NAME: tab:line-word-byte-count
#+CAPTION: Line, word, and byte counts
| 1485702 | 5626026 | 69141951 |

Table [[TestFiles]] contains some filenames we can analyze.

#+ATTR_LATEX: :align >{\ttfamily}l
#+NAME: TestFiles
#+CAPTION: List of files names.
| test_file.fbx            |
| armatures_and_things.fbx |

The downside of this is that you have to give up subscripts in the rest of your document. I thought maybe there was a way to limit this to a subtree with the EXPORT_OPTIONS but I couldn't get it to work. You can also play with the value of org-export-with-sub-superscripts (see https://emacs.stackexchange.com/a/10620/14373) if you're okay with using braces when you actually want Org to interpret an underscore as a subscript.

Michael M
  • 83
  • 4
  • I omitted to say that I'd rather not change the contents of the table because I want to feed them to a block of C code, where they are interpreted as strings. I can chop off the leading and trailing `=` signs, but that's kind of yucchy, because my C code will know about the special formatting I did to get the captions and references. See my edit above (in a few minutes after I comose it). – Reb.Cabin Jun 02 '17 at 02:54
  • Edited my answer a bit, I think it'll work for you? – Michael M Jun 02 '17 at 03:46
  • With `#+options: ^:nil`, the tables typeset acceptably and the captions and references go through. With `#+ATTR_LATEX :align >{\ttfamily}, the captions and references are gone again. Doesn't seem I can put an `#+ATTR_LATEX` on a table and get captions and references. In addition, I started to rather like the monofont in the table (because it's really input to a program), so I can create a new question: How to get captions, references, monofont, and unmodified content all together. It wouldn't be right to add conditions to this question, which you answered, however. – Reb.Cabin Jun 02 '17 at 12:30
  • Actually, I take it back! Your solution with `#+ATTR_LATEX :align >{\ttfamily}l` works perfectly on all counts! I had originally omitted to type in the trailing `l` `ell`. That is definitely a pesky letter in the alphabet. – Reb.Cabin Jun 02 '17 at 13:20
  • 1
    So I proposed this as an edit to the other answer but it seems maybe it's not the right place. The `:align {\ttfamily}l` is latex and specifies a one-column table. If you want to do this for a multicolumn table, you can use multiple copies of the same column format, i.e. `#+ATTR_LATEX: :align >{\ttfamily}l>{\ttfamily}l`. – Michael M Jun 02 '17 at 20:48
  • 1
    If you're going to be doing a lot of latex exporting, it's probably worth learning a little about the column specification for `tabular` environments (Org default) and maybe even other tabular environments (which you can specify in `#+attr_latex:`. https://en.wikibooks.org/wiki/LaTeX/Tables – Michael M Jun 02 '17 at 20:57
0

Building on Michael's answer, I created another MVE that shows all the above in action and presents one more unsolved problem.

#+OPTIONS: toc:nil
#+OPTIONS: ^:{}
#+LaTeX_HEADER: \usepackage{array}

* MINIMAL VIABLE EXAMPLE (MVE)

The goal is to get the strings in table [[TestFiles]] typeset by \LaTeX in monospace
font, while maintaining the following conditions:

1. The captions and cross references to table [[TestFiles]] continue to work
   - They stop working if we use =#+ATTR_LATEX :mode verbatim=
2. The data in table [[TestFiles]] works as input to the C code in figure
   [[just-open-and-close]], without the C code's having to manipulate the data

The first line of table [[TestFiles]] contains the file name of our input file; if
you want to analyze a different file, move its name to the top of the table.

#+ATTR_LATEX: :align >{\ttfamily}l
#+NAME: TestFiles
#+CAPTION: List of files names.
| test_file.fbx            |
| armatures_and_things.fbx |

Note carefully the trailing =l= (/ell/) in the =#+ATTR_LATEX= header. This
silently fails without that trailing ell.

Figure [[just-open-and-close]] contains
some code that uses the data in the file that is named on the first line of
table [[TestFiles]]. The output of this code appears in table [[output-from-code]].

#+NAME:    just-open-and-close
#+CAPTION: Program that opens and closes an input file
#+HEADER:  :exports  both
#+HEADER:  :flags    -std=c++14
#+HEADER:  :includes '(<stdio.h>)
#+HEADER:  :var      filenames   = TestFiles
#+BEGIN_SRC C++
  int main (int argc, char ** argv) {
      printf ("file_name: %s\n", filenames[0][0]);
      const auto fp = fopen (filenames[0][0], "r");
      printf ("file_pointer: %p\n", fp);
      fclose (fp);
  }
#+END_SRC

#+NAME:    output-from-code
#+CAPTION: Output from the code in figure [[TestFiles]]
#+RESULTS: just-open-and-close
| file_name:    |  test_file.fbx |
| file_pointer: | 0x7fff79937050 |

* UNSOLVED

Typesetting the results in table [[output-from-code]] in monofont is usolved. Adding

\noindent =#+ATTR_LATEX: :align >{\ttfamily}l=

\noindent to the table causes the =align= environment to cough up the error
message in figure [[unsolved-table-formatting-error]].

#+NAME: unsolved-table-formatting-error
#+CAPTION: Unsolved table-formatting error
#+HEADER:  :eval never
#+BEGIN_SRC
  Extra alignment tab has been changed to \cr.
  <recently read> \endtemplate

  l.82 file\_name: &
                     test\_file.fbx\\
  ! Extra alignment tab has been changed to \cr.
  <recently read> \endtemplate

  l.83 file\_pointer: &
                        0x7fff79937050\\
#+END_SRC
Reb.Cabin
  • 711
  • 6
  • 19
  • Please do not post an answer as a way to highlight a continued problem. Either post a comment on the relevant answer, or else ask a question in a separate post if it's warranted. – Dan Jun 02 '17 at 19:54
  • I made it a community wiki ... I thought that was the proper way to continue a discussion. – Reb.Cabin Jun 02 '17 at 20:42