auctex / etc / graphicx.el

stephent e717f65 

;;; graphicx.el --- AUCTeX style file for graphicx.sty

;; Copyright (C) 2000, 2004, 2005 by Free Software Foundation, Inc.

;; Author: Ryuichi Arafune <>
;; Created: 1999/3/20
;; Keywords: tex

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Commentary:

;;  This package supports the includegraphcics macro in graphicx style.

;; Acknowledgements
;;  Dr. Thomas Baumann <>
;;  David Kastrup <>
;;  Masayuki Akata <>

;;; Code:

 (lambda ()
    '("reflectbox" "Argument")
    '("resizebox" "Width" "Height" "Argument")
    '("resizebox*" "Width" "Total height" "Argument")
    '("rotatebox" [ "Options" ] "Angle" "Argument")
    '("scalebox" "Horizontal scale" [ "Vertical scale" ] "Argument")
    '("includegraphics" LaTeX-arg-includegraphics))
   ;; Fontification
   (when (and (featurep 'font-latex)
	      (eq TeX-install-font-lock 'font-latex-setup))
     (add-to-list 'font-latex-match-textual-keywords-local "reflectbox")
     ;; Fontification of optional and mandantory arguments of \resizebox,
     ;; \rotatebox and \scalebox should be improved.
     ;; (add-to-list 'font-latex-match-textual-keywords-local "resizebox")
     ;; (add-to-list 'font-latex-match-textual-keywords-local "rotatebox")
     ;; (add-to-list 'font-latex-match-textual-keywords-local "scalebox")
     (add-to-list 'font-latex-match-reference-keywords-local "includegraphics")
     ;; Tell font-lock about the update.
     (setq font-lock-set-defaults nil)

(defun LaTeX-includegraphics-extensions (&optional list)
  "Return appropriate extensions for input files to \\includegraphics."
  ;; FIXME: This function may check for latex/pdflatex later.
  (concat "\\."
	  (mapconcat 'identity
		     (or list LaTeX-includegraphics-extensions)

(defun LaTeX-includegraphics-read-file-TeX ()
  "Read image file for \\includegraphics.
Offers all graphic files found in the TeX search path.  See
`LaTeX-includegraphics-read-file' for more."
  ;; Drop latex/pdflatex differences for now.  Might be (re-)included later.
   "Image file: "
    (mapcar 'list
	    (TeX-search-files nil LaTeX-includegraphics-extensions t t)))
   nil nil nil))

(defun LaTeX-includegraphics-read-file-relative ()
  "Read image file for \\includegraphics.

Lists all graphic files in the master directory and its
subdirectories and inserts the relative file name.  This option
doesn't works with Emacs 21.3 or XEmacs.  See
`LaTeX-includegraphics-read-file' for more."
    "Image file: " nil nil nil nil
    ;; FIXME: Emacs 21.3 and XEmacs 21.4.15 dont have PREDICATE as the sixth
    ;; argument (Emacs 21.3: five args; XEmacs 21.4.15: sixth is HISTORY).
    (lambda (fname)
      (or (file-directory-p fname)
 	  (string-match (LaTeX-includegraphics-extensions) fname))))

(defun LaTeX-arg-includegraphics (prefix)
  "Ask for mandantory and optional arguments for the \\includegraphics command.

The extent of the optional arguments is determined by the prefix argument and
  (let* ((maybe-left-brace "[")
	 (maybe-comma "")
	 (image-file (funcall LaTeX-includegraphics-read-file))
	     (if (listp current-prefix-arg)
		 (setq current-prefix-arg (car current-prefix-arg))
	     (assq current-prefix-arg LaTeX-includegraphics-options-alist)))
	   ;; If no prefix is given, use `0' and tell the user about the
	   ;; prefix.
	   ((eq current-prefix-arg nil)
	    (setq show-hint t)
	    (cdr (assq 0 LaTeX-includegraphics-options-alist)))
	    (cdr (assq 0 LaTeX-includegraphics-options-alist)))))
	 ;; Order the optional aruments like in the tables in,
	 ;; page 14.  But collect y-or-n options at the end, so that the use
	 ;; can skip some options by typing `RET RET ... RET n n n ... n'
	 ;; Options from Table 1 (, page 14):
	   'totalheight incl-opts
	     (concat "Total Height (" TeX-default-unit-for-image "): "))))
	   'height incl-opts
	   ;; Either totalheight or height make sense:
	   '(when (zerop (length totalheight))
	       (concat "Figure height (" TeX-default-unit-for-image "): ")))))
	   'width incl-opts
	     (concat "Figure width (" TeX-default-unit-for-image "): "))))
	   'angle incl-opts
	   ;; If size is already specified, don't ask for scale:
	   '(when (zerop (+ (length totalheight)
			    (length height)
			    (length width)))
	      (read-input "Scale: "))))
	   'angle incl-opts
	   '(read-input "Rotation angle: ")))
	   'origin incl-opts
	      "Origin (any combination of `lcr' (horizontal) "
	      "and `tcbB' (vertical)): "))))
	 (page ;; Not in any table; Only for PDF.
	   'page incl-opts
	   '(read-input "Page: ")))
	   'bb incl-opts
	   '(y-or-n-p "Set Bounding Box? ")))
	 ;; Table 2:
	   'viewport incl-opts
	   '(y-or-n-p "Set viewport? ")))
	   'trim incl-opts
	   '(and (not viewport)
		 (y-or-n-p "Set trim? "))))
	 ;; Table 3:
	   'clip incl-opts
	   ;; If viewport, we also use clip.
	   '(or viewport
		(y-or-n-p "Clipping figure? "))))
	   'keepaspectratio incl-opts
	   ;; If we have width and [total]height...
	   '(or (and (not (zerop (length width)))
		     (or (not (zerop (length totalheight)))
			 (not (zerop (length height)))))
		(y-or-n-p "Keep Aspectratio? "))))
	 ;; Used for bb, trim, viewport, ...:
	 llx lly urx ury)
    ;; Now insert stuff...
    (when (not (zerop (length totalheight)))
       maybe-left-brace maybe-comma "totalheight="
       (car (TeX-string-divide-number-unit totalheight))
       (if (zerop
	     (car (cdr (TeX-string-divide-number-unit totalheight)))))
	 (car (cdr (TeX-string-divide-number-unit totalheight)))))
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when (not (zerop (length height)))
      (insert maybe-left-brace maybe-comma
	      "height=" (car (TeX-string-divide-number-unit height))
	      (if (zerop
		    (car (cdr (TeX-string-divide-number-unit height)))))
		(car (cdr (TeX-string-divide-number-unit height)))))
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when (not (zerop (length width)))
      (insert maybe-left-brace maybe-comma
	      "width=" (car (TeX-string-divide-number-unit width))
	      (if (zerop
		    (car (cdr (TeX-string-divide-number-unit width)))))
		(car (cdr (TeX-string-divide-number-unit width)))))
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when (not (zerop (length scale)))
      (insert maybe-left-brace maybe-comma "scale=" scale)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when (not (zerop (length angle)))
      (insert maybe-left-brace maybe-comma "angle=" angle)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when (not (zerop (length origin)))
      (insert maybe-left-brace maybe-comma "origin=" origin)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when bb
      (setq llx (read-input "Bounding Box lower left x: "))
      (setq lly (read-input "Bounding Box lower left y: "))
      (setq urx (read-input "Bounding Box upper right x: "))
      (setq ury (read-input "Bounding Box upper right y: "))
      (insert maybe-left-brace maybe-comma
	      "bb=" llx " " lly " " urx " " ury)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when viewport
      (setq llx (read-input "Viewport lower left x: "))
      (setq lly (read-input "Viewport lower left y: "))
      (setq urx (read-input "Viewport upper right x: "))
      (setq ury (read-input "Viewport upper right y: "))
      (insert maybe-left-brace maybe-comma
	      "viewport=" llx " " lly " " urx " " ury)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when trim
      (setq llx (read-input "Trim lower left x: "))
      (setq lly (read-input "Trim lower left y: "))
      (setq urx (read-input "Trim Upper right x: "))
      (setq ury (read-input "Trim Upper right y: "))
      (insert maybe-left-brace maybe-comma
	      "trim=" llx " " lly " " urx " " ury)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when clip
      (insert maybe-left-brace maybe-comma "clip")
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when keepaspectratio
      (insert maybe-left-brace maybe-comma "keepaspectratio")
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (when (not (zerop (length page)))
      (insert maybe-left-brace maybe-comma "page=" page)
      (setq maybe-comma ",")
      (setq maybe-left-brace ""))
    (if (zerop (length maybe-left-brace))
	(insert "]"))
    (TeX-insert-braces 0)
     (if LaTeX-includegraphics-strip-extension-flag
	 ;; We don't have `replace-regexp-in-string' in all (X)Emacs versions:
	   (insert image-file)
	   (goto-char (point-max))
	   (when (search-backward-regexp (LaTeX-includegraphics-extensions)
					 nil t 1)
	     (replace-match ""))
    (when show-hint
	"Adding `C-u C-u' before the command asks for more optional arguments."
	"\nSee `LaTeX-includegraphics-options-alist' for details."))
      (sit-for 3))

;;; graphicx.el ends here