diff --git a/action.php b/action.php
index 8147da662650586a98a03a77561c9c7ba3840d0a..39c9b41ea2e4cc042b374273fa9fe9ee993a72fa 100644
--- a/action.php
+++ b/action.php
@@ -606,7 +606,7 @@ if ($action === 'getCommentsToPrint') {
         $count = 0;
         foreach ($conversations as $conversation) {
             $post = new stdClass();
-            $post->answeredquestion = pdfannotator_handle_latex($conversation->answeredquestion);
+            $post->answeredquestion = pdfannotator_handle_latex($context, $conversation->answeredquestion);
             $post->page = $conversation->page;
             $post->annotationtypeid = $conversation->annotationtypeid;
             $post->author = $conversation->author;
@@ -616,7 +616,7 @@ if ($action === 'getCommentsToPrint') {
             $answercount = 0;
             foreach ($conversation->answers as $ca) {
                 $answer = new stdClass();
-                $answer->answer = pdfannotator_handle_latex($ca->answer);
+                $answer->answer = pdfannotator_handle_latex($context, $ca->answer);
                 $answer->author = $ca->author;
                 $answer->timemodified = $ca->timemodified;
                 $post->answers[$answercount] = $answer;
diff --git a/constants.php b/constants.php
index c00d947ebe13115d8767a27e10c259ce9ba869e1..56e8f3089fddc7e658fe6c6fc2c88594ae360dcc 100644
--- a/constants.php
+++ b/constants.php
@@ -28,6 +28,13 @@ defined('MOODLE_INTERNAL') || die();
  */
 define('LATEX_TO_PNG_REQUEST',   'https://chart.googleapis.com/chart?cht=tx&chl=');
 
+/**
+ * Define whether to use googleapi or moodle Latex Library
+ */
+define('LATEX_TO_PNG_MOODLE', 0);
+define('LATEX_TO_PNG_GOOGLE_API', 1);
+
+
 /**
  * Prefix needed for encode64 imaged
  */
diff --git a/lang/en/pdfannotator.php b/lang/en/pdfannotator.php
index c938adea875b601f2974a2809ce442f49ec65b88..b0b8288a15153acd99ad213f434b0c72026b035b 100644
--- a/lang/en/pdfannotator.php
+++ b/lang/en/pdfannotator.php
@@ -145,6 +145,10 @@ $string['fullscreen'] = 'Fullscreen';
 $string['fullscreenBack'] = 'Exit Fullscreen';
 $string['global_setting_anonymous'] = 'Allow anonymous posting?';
 $string['global_setting_anonymous_desc'] = 'With this option you allow your user to post comments anonymously. This option activates anonymous posting globally';
+$string['global_setting_latexapisetting'] = 'LaTeX to PNG API';
+$string['global_setting_latexapisetting_desc'] = 'API for converting Latex to PNG for PDF Downloads. Note: If you use the Google Chart API, Google will get all formulas in the document if someone chooses to use LaTeX';
+$string['global_setting_latexusemoodle'] = 'Internal Moodle API';
+$string['global_setting_latexusegoogle'] = 'Google Chart API';
 $string['global_setting_use_studentdrawing'] = 'Allow drawings for participants?';
 $string['global_setting_use_studentdrawing_desc'] = 'Please note that drawings are anonymous and can neither be commented nor reported.';
 $string['global_setting_use_studenttextbox'] = 'Allow textboxes for participants?';
diff --git a/locallib.php b/locallib.php
index 86f70e9b72413b0f332de0b93a1ce199e8a70266..e02d956d85b6ae55ee416471f758440c6a15041d 100644
--- a/locallib.php
+++ b/locallib.php
@@ -132,17 +132,16 @@ function pdfannotator_get_annotationtype_name($typeid) {
     }
 }
 
-function pdfannotator_handle_latex($subject) {
-
+function pdfannotator_handle_latex($context, string $subject) {
     global $CFG;
     require_once($CFG->dirroot . '/mod/pdfannotator/constants.php');
+    $latexapi = get_config('mod_pdfannotator','latexapi');
 
     // Look for these formulae: $$ ... $$, \( ... \) and \[ ... \]
     // !!! keep indentation!
     $pattern = <<<'SIGN'
 ~(?:\$\$.*?\$\$)|(?:\\\(.*?\\\))|(?:\\\[.*?\\\])~
 SIGN;
-    // Working, but less readable, alternative: $pattern = '~(?:\\$\\$.*?\\$\\$)|(?:\\\\\\(.*?\\\\\\))|(?:\\\\\\[.*?\\\\\\])~';
 
     $matches = array();
     $hits = preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
@@ -160,7 +159,11 @@ SIGN;
         $formulalength = strlen($match[0]);
         $formulaoffset = $match[1];
         $result[] = trim(substr($subject, $textstart, $formulaoffset - $textstart));
-        $result[] = pdfannotator_process_latex($match[0]);
+        if($latexapi == LATEX_TO_PNG_GOOGLE_API) {
+            $result[] = pdfannotator_process_latex_google($match[0]);
+        } else {
+            $result[] = pdfannotator_process_latex_moodle($context, $match[0]);
+        }
         $textstart = $formulaoffset + $formulalength;
     }
     if ($textstart != strlen($subject) - 1) {
@@ -169,6 +172,22 @@ SIGN;
     return $result;
 }
 
+function pdfannotator_process_latex_moodle($context, $string) {
+    global $CFG;
+    require_once($CFG->libdir . '/moodlelib.php');
+    require_once($CFG->dirroot . '/filter/tex/latex.php');
+    require_once($CFG->dirroot . '/filter/tex/lib.php');
+    $result = array();
+    $tex = new latex();
+    $md5 = md5($string);
+    $image = $tex->render($string, $md5 . 'png');
+    $imagedata = file_get_contents($image);
+    $result['image'] = IMAGE_PREFIX . base64_encode($imagedata);
+    //imageinfo returns an array with the info of the size of the image. In Parameter 1 there is the height, which is the only thing needed here
+    $imageinfo = getimagesize($image);
+    $result['imageheight'] = $imageinfo[1];
+    return $result;
+}
 /**
  * Function takes a latex code string, modifies and url encodes it for the Google Api to process,
  * and returns the resulting image along with its height
@@ -176,7 +195,7 @@ SIGN;
  * @param type $string
  * @return type
  */
-function pdfannotator_process_latex($string) {
+function pdfannotator_process_latex_google(string $string) {
     $string = str_replace('\xrightarrow', '\rightarrow', $string);
     $string = str_replace('\xlefttarrow', '\leftarrow', $string);
 
diff --git a/renderer.php b/renderer.php
index 0bc065536d2df39bfe67cc5479771fa68a67297b..a18ca0941f9171edbcc067d5eb8408701d8ab31c 100644
--- a/renderer.php
+++ b/renderer.php
@@ -23,8 +23,6 @@
  */
 defined('MOODLE_INTERNAL') || die();
 
-require_once('../../config.php');
-
 class mod_pdfannotator_renderer extends plugin_renderer_base {
 
     /**
diff --git a/settings.php b/settings.php
index 92e1d216d17296084f4c4cbdc5e8eaa9a8b9683b..dd36a4bec191f584f2c6009da9c4291b42dd3bdd 100644
--- a/settings.php
+++ b/settings.php
@@ -22,7 +22,7 @@
 defined('MOODLE_INTERNAL') || die; // Prevents crashes on misconfigured production server.
 
 if ($ADMIN->fulltree) {
-
+    require_once('constants.php');
     $settings->add(new admin_setting_configcheckbox('mod_pdfannotator/usevotes',
             get_string('global_setting_usevotes', 'pdfannotator'), get_string('global_setting_usevotes_desc', 'pdfannotator'), 1));
 
@@ -39,4 +39,11 @@ if ($ADMIN->fulltree) {
     $settings->add(new admin_setting_configcheckbox('mod_pdfannotator/use_studentdrawing',
             get_string('global_setting_use_studentdrawing', 'pdfannotator'),
             get_string('global_setting_use_studentdrawing_desc', 'pdfannotator'), 0));
+
+    //Define what API to use for converting latex formulas into png.
+    $options = array();
+    $options[LATEX_TO_PNG_MOODLE] = get_string("global_setting_latexusemoodle", "pdfannotator");
+    $options[LATEX_TO_PNG_GOOGLE_API] = get_string("global_setting_latexusegoogle", "pdfannotator");
+    $settings->add(new admin_setting_configselect('mod_pdfannotator/latexapi', get_string('global_setting_latexapisetting', 'pdfannotator'),
+        get_string('global_setting_latexapisetting_desc', 'pdfannotator'), LATEX_TO_PNG_MOODLE, $options));
 }
\ No newline at end of file