Hi,

I've been struggling with Reftex in org-mode. Initially things are fine,
then after exporting the document to LaTeX and continuing to work with
org-mode, using most of the reftex features (e.g. reftex-toc or trying to
insert a reference) produces a "Wrong type argument: stringp, nil" error
message.

The code in my init file to set up reftex:

    ;; Reftex
    (require 'reftex)
    (defun org-mode-reftex-setup ()
      (load-library "reftex")
      (and (buffer-file-name) (file-exists-p (buffer-file-name))
         ;enable auto-revert-mode to update reftex when bibtex file changes on disk
         (global-auto-revert-mode t)
         (reftex-parse-all)
          (define-key org-mode-map (kbd "C-c [") 'reftex-citation)
         ;add a custom reftex cite format to insert links
         ;; (reftex-set-cite-format "** [[papers:%l][%l]]: %t \n")
         )
    ) 

    (add-hook 'org-mode-hook 'org-mode-reftex-setup) 

At the moment I still enter reftex-mode on demand with "M-x reftex-mode".
Here is the backtrace for the error:

Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  re-search-forward(nil 10001 t)
  tex-main-file()
  reftex-TeX-master-file()
  reftex-insert-docstruct(#<buffer paper.org> t "e" nil nil t nil nil ("SEC:lagrange_gibbs_proof" "s" "We define the molar Gibbs free energy in a molecular gas mixture as \\begin{align} \\h" "/my/path/to/the/paper.org" nil) "" nil)
  byte-code("\306 \307\216\310 \210 \311 \f\203\312r q\210\313
!)!\210\202&\312\314!\210\315 \210@\316=\2040\316 \210\317\320p\"\210\321A\322\323\324\325\326B\327\330\331\332\333\257 C\334 \335U\203v\336D\337\340!\210\341 E
\336FGHIJ\206kKL\336& K)\202\211J\203\206\342 J
#K\202\211\321K\321DK\206\222\321K\336J\343MNOK\344P&\211Q@RQA@S\345Q8T\321KR\204\304\346\347\336\"\210R\350=\203\321\315 \210\202 R\351=\204\337R\352=\203\365U\203\347\352R\315 \210\353 TR#\210\202 R\354=\203\315 \210G?G\202 R\355=\203VJ\356 \202 R\357=\2038\315 \210U\2030\360U!W\202 E?E\202 R\361=\203J\315 \210F?F\202 R\362=\203\\\315 \210H?H\202 R\363=\203n\315 \210I?I\202 R\364=\203\201XY\346\347\321\"\210\202 R\365=\203\264\366Z[\"[\367[Z8A!\206\236\370\371![Z8@\206\251\372L q\210\373 \210\202 R;\203\324\374R\\J\"\206\310R
DCCY\346\347\321\"\210\202 R\375>\203]\203\350]\237Y\202\371S\203\366SCCY\202\371\336YY\203R\376\232\203 \377YBYYX q\210\346\347\321\"\210\202 \370\201^!\210*\202" [wconfig buf reftex-call-back-to-this-buffer reftex-latex-syntax-table reftex-use-multiple-selection-buffers typekey current-window-configuration ((set-window-configuration wconfig)) delete-other-windows syntax-table switch-to-buffer-other-window reftex-make-selection-buffer-name "*RefTeX Select*" reftex-erase-buffer reftex-select-label-mode add-to-list selection-buffers t "----  " mode-line-buffer-identification "  " global-mode-string "   (" ")" "  S<" reftex-refstyle ">" " -%-" buffer-size 0 nil message "Creating Selection Buffer..." reftex-insert-docstruct reftex-get-offset reftex-select-item reftex-show-label-location 2 throw exit 103 114 82 reftex-reparse-document 99 115 reftex-query-label-type 116 prefix-numeric-value 70 ...] 13)
  reftex-offer-label-menu("e")
  reftex-reference()
  call-interactively(reftex-reference nil nil)

Many thanks,
Adam



Emacs  : GNU Emacs 24.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.36)
 of 2013-03-13 on bob.porkrind.org
Package: Org-mode version 8.2.10 (8.2.10-35-g19a7d6-elpa @ /Users/adamjackson/.emacs.d/elpa/org-20150323/)

current state:
==============
(setq
 org-export-backends '(latex md ascii html icalendar beamer)
 org-tab-first-hook '(org-hide-block-toggle-maybe org-src-native-tab-command-maybe
                      org-babel-hide-result-toggle-maybe org-babel-header-arg-expand)
 org-latex-classes '(("beamer" "\\documentclass[presentation]{beamer}" ("\\section{%s}" . "\\section*{%s}")
                      ("\\subsection{%s}" . "\\subsection*{%s}") ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
                     ("article" "\\documentclass[11pt]{article}" ("\\section{%s}" . "\\section*{%s}")
                      ("\\subsection{%s}" . "\\subsection*{%s}") ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                      ("\\paragraph{%s}" . "\\paragraph*{%s}") ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
                     ("report" "\\documentclass[11pt]{report}" ("\\part{%s}" . "\\part*{%s}")
                      ("\\chapter{%s}" . "\\chapter*{%s}") ("\\section{%s}" . "\\section*{%s}")
                      ("\\subsection{%s}" . "\\subsection*{%s}") ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
                     ("book" "\\documentclass[11pt]{book}" ("\\part{%s}" . "\\part*{%s}")
                      ("\\chapter{%s}" . "\\chapter*{%s}") ("\\section{%s}" . "\\section*{%s}")
                      ("\\subsection{%s}" . "\\subsection*{%s}") ("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
                     )
 org-speed-command-hook '(org-speed-command-default-hook org-babel-speed-command-hook)
 org-occur-hook '(org-first-headline-recenter)
 org-metaup-hook '(org-babel-load-in-session-maybe)
 org-html-format-drawer-function '(lambda (name contents) contents)
 org-log-done 'time
 org-latex-format-inlinetask-function 'ignore
 org-confirm-shell-link-function 'yes-or-no-p
 org-ascii-format-inlinetask-function 'org-ascii-format-inlinetask-default
 org-agenda-custom-commands '(("h" "\"home\": Agenda and unscheduled non-work TODOs"
                               ((agenda "" nil) (tags-todo "-work-SCHEDULED={.+}" nil)) nil nil)
                              ("H" "\"Home\": Non-work unscheduled TODOs" tags "-work-SCHEDULED={.+}" nil)
                              ("w" "Unscheduled :work:" tags "work-SCHEDULED={.+}" nil)
                              ("n" "Agenda and all TODOs" ((agenda "" nil) (alltodo "" nil)) nil))
 org-latex-format-headline-function 'org-latex-format-headline-default-function
 org-after-todo-state-change-hook '(org-clock-out-if-current)
 org-latex-format-drawer-function '(lambda (name contents) contents)
 org-src-mode-hook '(org-src-babel-configure-edit-buffer org-src-mode-configure-edit-buffer)
 org-agenda-before-write-hook '(org-agenda-add-entry-text)
 org-babel-pre-tangle-hook '(save-buffer)
 org-agenda-dim-blocked-tasks 'invisible
 org-mode-hook '((lambda nil
                  (org-add-hook (quote change-major-mode-hook) (quote org-show-block-all) (quote append)
                   (quote local))
                  )
                 (lambda nil
                  (org-add-hook (quote change-major-mode-hook) (quote org-babel-show-result-all) (quote append)
                   (quote local))
                  )
                 #[nil "\300\301\302\303\304$\207"
                   [org-add-hook change-major-mode-hook org-show-block-all append local] 5]
                 #[nil "\300\301\302\303\304$\207"
                   [org-add-hook change-major-mode-hook org-babel-show-result-all append local] 5]
                 org-babel-result-hide-spec org-babel-hide-all-hashes org-mode-reftex-setup)
 org-ascii-format-drawer-function '(lambda (name contents width) contents)
 org-ctrl-c-ctrl-c-hook '(org-babel-hash-at-point org-babel-execute-safely-maybe)
 org-directory "~/Dropbox/org"
 org-enforce-todo-dependencies t
 org-cycle-hook '(org-cycle-hide-archived-subtrees org-cycle-hide-drawers org-cycle-hide-inline-tasks
                  org-cycle-show-empty-lines org-optimize-window-after-visibility-change)
 org-babel-tangle-lang-exts '(("python" . "py") ("emacs-lisp" . "el"))
 org-confirm-elisp-link-function 'yes-or-no-p
 org-metadown-hook '(org-babel-pop-to-session-maybe)
 org-blocker-hook '(org-block-todo-from-children-or-siblings-or-parent)
 org-html-format-headline-function 'ignore
 org-babel-load-languages '((python . t))
 org-html-format-inlinetask-function 'ignore
 org-agenda-files '("~/Dropbox/org/todo.org" "~/Dropbox/org/shopping.org")
 org-clock-out-hook '(org-clock-remove-empty-clock-drawer)
 )